Query profiling and the query plan in Snowflake - Time & Space Complexity
When we run a query in Snowflake, it goes through steps that take time and resources.
We want to understand how the time to run a query grows as the data or query size grows.
Analyze the time complexity of the following query profiling operation.
-- Run a query
SELECT * FROM sales WHERE region = 'North';
-- Get query ID for profile
SELECT LAST_QUERY_ID();
This sequence runs a query and then retrieves its detailed profile to understand execution steps.
Look at what happens repeatedly during profiling and query execution.
- Primary operation: Scanning data rows and processing query steps.
- How many times: Depends on data size and query complexity; each row and step is processed once.
As the amount of data or query complexity grows, the number of operations grows roughly in proportion.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 rows | About 10 scan and process steps |
| 100 rows | About 100 scan and process steps |
| 1000 rows | About 1000 scan and process steps |
Pattern observation: The work grows linearly as data size grows.
Time Complexity: O(n)
This means the time to profile and run the query grows roughly in direct proportion to the data size.
[X] Wrong: "Profiling a query takes the same time no matter how big the data is."
[OK] Correct: Profiling depends on the query execution steps, which increase with data size and complexity, so it takes more time for bigger data.
Understanding how query profiling time grows helps you explain performance issues clearly and shows you know how cloud data systems work under the hood.
"What if we changed the query to use a filter that reduces data scanned by half? How would the time complexity change?"
Practice
EXPLAIN command in Snowflake do?Solution
Step 1: Understand the purpose of EXPLAIN
The EXPLAIN command shows the query plan, which details how Snowflake will execute the query step-by-step without running it.Step 2: Differentiate EXPLAIN from other commands
Unlike running the query or deleting history, EXPLAIN only provides the plan, not results or changes.Final Answer:
Shows the steps Snowflake will take to run a query -> Option AQuick Check:
EXPLAIN = Shows query plan [OK]
- Thinking EXPLAIN runs the query
- Confusing EXPLAIN with query deletion
- Assuming EXPLAIN optimizes automatically
Solution
Step 1: Recall Snowflake syntax for query plans
Snowflake uses the EXPLAIN keyword before a query to show its plan.Step 2: Verify syntax correctness
OnlyEXPLAIN SELECT ...is valid; other options are not recognized commands.Final Answer:
EXPLAIN SELECT * FROM my_table; -> Option BQuick Check:
EXPLAIN + query = correct syntax [OK]
- Using SHOW PLAN which is not valid in Snowflake
- Trying DESCRIBE QUERY which is incorrect
- Using PLAN QUERY which is not a command
Solution
Step 1: Interpret rows scanned and cost in query plan
A large number of rows scanned and high cost means this step requires more work and resources.Step 2: Understand impact on query performance
Such steps usually slow down the query and increase resource usage, not speed it up or skip it.Final Answer:
This step may slow down the query and use more resources -> Option CQuick Check:
High rows scanned = slower step [OK]
- Assuming high cost means faster query
- Thinking Snowflake skips costly steps
- Confusing cost with query result count
EXPLAIN SELECT * FROM orders WHERE order_date > '2023-01-01'; but the plan shows a full table scan. What is a likely reason?Solution
Step 1: Analyze why full table scan occurs
Full table scans happen when no index or clustering helps filter data efficiently.Step 2: Check other options for validity
The query syntax is correct, Snowflake supports date filters, and an empty table would not cause a full scan.Final Answer:
There is no index or clustering onorder_date-> Option AQuick Check:
No index = full scan [OK]
- Thinking syntax error causes full scan
- Believing Snowflake can't filter dates
- Assuming empty table causes full scan
Solution
Step 1: Understand data shuffle in join operations
Data shuffle happens when data must move between nodes to match join keys, which is costly.Step 2: Use clustering keys to optimize joins
Clustering keys organize data so join keys are stored together, reducing shuffle and improving speed.Step 3: Evaluate other options
Adding columns or removing filters increases data processed; increasing warehouse size helps but does not reduce shuffle.Final Answer:
Use clustering keys to co-locate join keys and reduce data shuffle -> Option DQuick Check:
Clustering keys reduce shuffle cost [OK]
- Adding columns increases data, not reduces cost
- Removing WHERE clause slows query
- Relying only on warehouse size ignores shuffle
Ready to prove it?
Pass the test and this pattern moves unknown
