What you will be able to do
- Read a Query Profile's steps, operator tree, operator nodes and operator types to find the expensive part of a query
- Use EXPLAIN to see a query's execution path and tell compile-time pruning apart from runtime optimizations
- Spot exploding joins, non-equality joins and an unneeded UNION from their operators
- Write effective joining conditions: one per joined table, on a key, with equality
- Diagnose local and remote spilling and choose the right fix
- Recognise when partition pruning is not happening and correct the predicate or the data layout
- Tell a result cache, metadata or warehouse cache hit apart in a profile, and explain a slow first run after a warehouse resumes
- Set STATEMENT_TIMEOUT_IN_SECONDS and STATEMENT_QUEUED_TIMEOUT_IN_SECONDS at the right level
Key concept
Query Profile operator tree — A query runs as one or more steps. Each step is a tree of operator nodes such as TableScan, Filter, Join, Aggregate and Sort, and every node carries its own statistics. To tune a query, you find the node where the time, rows, spilled bytes or scanned partitions pile up, and fix that node.
1.Steps, operator nodes and the operator tree
A query that is slow or expensive shows you why in its Query Profile. In Snowsight, open Monitoring » Query History, select the query ID, and open the Query Profile tab. The profile has a Most Expensive Nodes pane that lists the operators taking the longest. For each node you can also see what share of its time went to each category of processing, so you know where to look before you read the whole plan.
The profile is organised in three layers. A step is a separately executed part of the query. Most queries have one step, and some run as several. Inside a step, operator nodes form a tree. Each parent is drawn above its children, and data flows up from the TableScans at the bottom to the Result at the top. The operator type of a node tells you what it does, and its attributes and statistics tell you how much work it did.
| Operator type | What it does | What to look at |
|---|---|---|
| TableScan | Represents access to a single table | Partitions scanned vs Partitions total; percentage scanned from cache |
| Filter | Filters records on a condition | How many rows it removes above a TableScan |
| Join | Combines two inputs on a condition | Equality Join Condition, Additional Join Condition, rows out vs rows in |
| JoinFilter | Removes tuples that cannot match a later Join | Which join it serves (Original join ID) |
| Aggregate | GROUP BY or SELECT DISTINCT | Grouping Keys and aggregate functions |
| Sort / SortWithLimit | ORDER BY, or ORDER BY ... LIMIT ... OFFSET | Sort keys and number of rows produced |
| UnionAll | Concatenates two inputs | Whether an Aggregate sits on top of it |
| Metadata-based Result | Answers the query from metadata only | No warehouse was used |
The same statistics are available in SQL. The GET_QUERY_OPERATOR_STATS table function returns one row per operator, with columns STEP_ID, OPERATOR_ID, PARENT_OPERATORS and OPERATOR_TYPE. The OPERATOR_STATISTICS column holds input_rows, output_rows and a pruning object with partitions_scanned and partitions_total. Use the function when you want to check many queries, or track one query over time, without clicking through Snowsight.
Checkpoint 1 of 8· Check yourself
A slow query keeps coming back. You want each operator's input rows, output rows and partitions scanned for that query over the past month, without opening the profile in Snowsight every time. What should you use?
GET_QUERY_OPERATOR_STATS gives you the Query Profile's per-operator statistics in SQL: STEP_ID, OPERATOR_ID, OPERATOR_TYPE, and OPERATOR_STATISTICS such as input_rows, output_rows and pruning. That lets you check many queries, or follow one query over time. RESULT_SCAN returns a query's result rows, not its profile. The Most Expensive Nodes pane is part of the Snowsight profile view, so you would still open each query by hand.
“You can programmatically access the performance statistics of the Query Profile by executing the GET_QUERY_OPERATOR_STATS function.”Source: docs.snowflake.com
2.EXPLAIN, the execution path and compile-time vs runtime optimization
EXPLAIN shows you a query's execution path before you pay for it. It returns the logical plan, meaning the operations Snowflake would perform and how they relate. Each row has a step, an id, its parentOperators, the operation (Result, Filter, TableScan, Join...) and the expressions involved. Compiling the plan costs Cloud Services credits, but no warehouse credits. If no warehouse is current, Snowflake builds the plan as if the warehouse were XSMALL, so the plan can differ from the one your real warehouse would get.
EXPLAIN [ USING { TABULAR | JSON | TEXT } ] <statement>Snowflake optimizes a query at two points, and EXPLAIN only shows the first. Compile-time optimization appears in EXPLAIN as partitionsAssigned: the partitions left after compile-time pruning, out of partitionsTotal. That number is an upper bound. Runtime optimizations, such as join pruning, can cut partitions and bytes further while the query runs, and you see that only in the Query Profile afterwards. The logical order in the plan is also not a promise of execution order: the actual order of operations may differ.
Comparing compile versus runtime optimizations tells you where a fix belongs. Compile pruning works from the SQL text and the table's metadata alone. If partitionsAssigned is close to partitionsTotal for a query that should touch one day of data, the filter as written is not pruning, and the fix is in the predicate or the data layout, not the warehouse. Runtime optimizations act on data the query meets as it executes. A JoinFilter in the plan is one of them: it sits below a Join and removes tuples that cannot possibly match that Join's condition further up. EXPLAIN shows that the JoinFilter exists. How many partitions and rows it saved shows up only in the Query Profile once the query has run.
Checkpoint 2 of 8· Check yourself
EXPLAIN on a two-table join reports partitionsAssigned = 1,200 out of partitionsTotal = 5,000 for the fact table. After the query runs, the TableScan node in the Query Profile shows only 300 partitions scanned. What explains the gap?
partitionsAssigned is what remains after compile-time pruning. Snowflake can prune more partitions at run time, for example through join pruning, and you see that only in the Query Profile after the query runs. The figure from before pruning is partitionsTotal, not partitionsAssigned. Data read from cache is still scanned; the profile reports it as a percentage scanned from cache.
“The micro-partition metadata maintained by Snowflake enables precise pruning of columns in micro-partitions at query run-time”Source: docs.snowflake.com
3.Joins, grouping and sorting that the profile flags
Most avoidable slowness in a profile comes from the SQL itself, not the warehouse. The most expensive mistake is the exploding join. A missing join condition (a Cartesian product), or one where each row matches many rows on the other side, makes the Join operator produce far more rows than it reads, and that Join usually also uses a lot of time. Compare its output rows with its input rows to spot it. To scan for this across a whole query, divide output_rows by input_rows for each Join operator.
SELECT operator_id,
operator_attributes,
operator_statistics:output_rows / operator_statistics:input_rows AS row_multiple
FROM TABLE(GET_QUERY_OPERATOR_STATS($lid))
WHERE operator_type = 'Join'
ORDER BY step_id, operator_id;Effective joining conditions match each row to as few rows as possible, and they compare with equality. Give every table in the FROM clause a condition, so no pair of inputs is left to form a Cartesian product. Join on a key that identifies one row on at least one side, such as sales.order_id = orders.order_id. A condition on a column like region, shared by thousands of rows on both sides, lets each row match thousands, and the Join explodes even though the condition is technically present. A Join shows its equality predicates under Equality Join Condition, and any other predicates under Additional Join Condition. Non-equality predicates can make processing much slower, so after editing a join, check which list each predicate landed in. Wherever the logic allows, move the matching onto equality keys.
Grouping and set operations show up as their own operators. GROUP BY and SELECT DISTINCT both become an Aggregate. ROLLUP, CUBE and GROUPING SETS become GroupingSets. ORDER BY becomes a Sort, and ORDER BY with LIMIT becomes SortWithLimit, which produces only the rows it needs. UNION removes duplicates and UNION ALL does not. A UNION therefore appears as a UnionAll with an extra Aggregate on top doing the deduplication. If duplicates cannot occur, or don't matter, use UNION ALL and that Aggregate disappears.
Checkpoint 3 of 8· Check yourself
A report query has become very slow since someone edited its joins. You suspect that one Join is emitting far more rows than it reads, but the plan has dozens of operators. Which approach finds every such Join without clicking through each node in Snowsight?
An exploding join shows up when a Join's output rows are far higher than its input rows. GET_QUERY_OPERATOR_STATS returns that statistic for every operator in SQL, so you can compute the ratio for all Joins at once. Partition pruning and Filter selectivity point to other problems, and the slowest node shows where time went but not whether a join multiplied the rows.
“You can programmatically access the performance statistics of the Query Profile by executing the GET_QUERY_OPERATOR_STATS function.”Source: docs.snowflake.com
Sources3
4.Data spilling: local, then remote
Spilling happens when an operation's intermediate results don't fit in the memory of the servers running it. Large joins, aggregations and deduplication over a huge data set are typical causes. The impact on performance is drastic, and it gets worse in stages. The engine first spills to local disk. If local disk also fills, it spills to remote cloud storage, and remote spilling slows a query far more than local spilling. To identify the operator responsible, select the node in the Query Profile that shows Bytes spilled to local storage or Bytes spilled to remote storage.
The two solutions are more memory per operation or less data per operation. Run the query on a larger warehouse, which gives each operation more memory and local disk, or process the data in smaller batches. Picture a nightly 4 TB join on a MEDIUM warehouse that spills remotely in its Join and Aggregate nodes. Moving it to a larger size is the first step. A lower timeout only cancels the query sooner. Search optimization speeds up selective lookups and adds no memory to a join. Choosing the warehouse size itself is covered in warehouse design.
SELECT query_id, SUBSTR(query_text, 1, 50) partial_query_text, user_name, warehouse_name,
bytes_spilled_to_local_storage, bytes_spilled_to_remote_storage
FROM snowflake.account_usage.query_history
WHERE (bytes_spilled_to_local_storage > 0
OR bytes_spilled_to_remote_storage > 0 )
AND start_time::date > dateadd('days', -45, current_date)
ORDER BY bytes_spilled_to_remote_storage, bytes_spilled_to_local_storage DESC
LIMIT 10;Checkpoint 4 of 8· Fill the gap
This ACCOUNT_USAGE query should list the worst-spilling queries of the last 45 days, including queries that spilled past local disk. Which column completes the filter?
SELECT query_id, SUBSTR(query_text, 1, 50) partial_query_text, user_name, warehouse_name,
bytes_spilled_to_local_storage, bytes_spilled_to_remote_storage
FROM snowflake.account_usage.query_history
WHERE (bytes_spilled_to_local_storage > 0
OR ? > 0 )
AND start_time::date > dateadd('days', -45, current_date)
ORDER BY bytes_spilled_to_remote_storage, bytes_spilled_to_local_storage DESC
LIMIT 10;Spilling happens at two levels. Data goes to local disk first, then to remote cloud storage once local disk fills. The filter therefore needs both spill columns to catch every spilling query. Remote spilling is the more serious of the two. Cache percentage, partitions scanned and bytes scanned measure caching, pruning and scan volume, not memory pressure.
Source: docs.snowflake.comCheckpoint 5 of 8· Exam question
A nightly aggregation on a MEDIUM warehouse joins a 4 TB fact table to a large dimension table and runs for hours. The Query Profile shows 'Bytes spilled to remote storage' in the Join and Aggregate operators and very high Remote Disk IO time. Which action is the MOST appropriate first step?
Correct answer: A — Run the query on a larger warehouse so each node has more memory and local disk for intermediate join result data
- A. Correct. Remote spilling means intermediate results exceeded both memory and local SSD, so a larger warehouse (more memory and local disk per query) or reducing the data processed by filtering earlier removes the spill.
- B. Incorrect. Search optimization accelerates selective point lookups and does not reduce the memory needed by a large join or aggregation, so spilling continues.
- C. Incorrect. A statement timeout only cancels long-running statements; it does nothing to give the join more memory and simply makes the nightly job fail.
- D. Incorrect. Multi-cluster warehouses add clusters for concurrency, but a single query still runs on one cluster, so its memory shortage and spilling remain.
5.When partition pruning or caching is not helping a scan
Pruning lets Snowflake skip micro-partitions that cannot contain matching rows. For every micro-partition it records the range of values in each column, and it compares the query's filter against those ranges. In the profile, open the TableScan and compare Partitions scanned with Partitions total. If scanned is a small fraction of total, pruning is working. If the two are close, it did nothing. A Filter above the TableScan that removes many rows while the scan read almost everything means the data layout or the predicate needs to change.
Pruning has two requirements. First, the order in which data is stored must correlate with the filter columns. Second, the predicate must be one Snowflake can test against the stored ranges, and not every predicate expression qualifies. A predicate with a subquery, for example, does not prune even if the subquery returns a constant. Compare the range metadata on event_ts with a filter such as WHERE TO_VARCHAR(event_ts, 'YYYY-MM-DD') = '2026-09-30'. The filter tests a converted string, not the column itself. Write it as a range on the column instead: event_ts >= '2026-09-30' AND event_ts < '2026-10-01'. Then the filter can be checked directly against each partition's min and max values.
Pruning doesn't wait for any background job, and it doesn't depend on caches or warehouse size. Clustering metadata is collected for each micro-partition as data is loaded, and compile-time pruning happens during compilation. When the predicate is already in a form that prunes and the scan still reads most partitions, the data is badly ordered for that filter. That is the case for a clustering key, which defines a sort order that Snowflake maintains for the table. See Clustering keys and automatic clustering. Clustering depth measures how much micro-partitions overlap on the given columns, with a minimum of 1 for a populated table. The smaller the depth, the better clustered the table. Query performance remains the final test.
A query that is fast on one run and slow on the next, with the same plan and the same pruning, is one of the most common query performance issues to troubleshoot. What changed is which cache answered. Different caching techniques leave different marks in the profile, and those marks show how much impact caching had on performance:
- Query Result Reuse as the only operator: the result cache returned a stored result, so the query bypassed execution entirely. This wins for an identical query over unchanged data.
- Metadata-based Result: an answer such as SELECT COUNT(*) came from metadata alone, and no virtual warehouse processed it.
- Percentage scanned from cache on a TableScan: the query did run on the warehouse, but read part of its table data from the warehouse's local disk cache instead of from storage. This helps similar queries that are not identical.
The local disk cache is the one that comes and goes, and suspending the warehouse is what clears it. When a warehouse is suspended, its cache is dropped. After it resumes, the first query reads its data from table storage, and its TableScan shows little or nothing scanned from cache. The same query ten minutes later, on the still-running warehouse, can read most of its data from cache. A slow 9:00 run on a warehouse with a short AUTO_SUSPEND is that cold cache, not a pruning or plan problem. Rewriting the SQL won't change it; the suspend window will.
Checkpoint 6 of 8· Check yourself
Partitions scanned on a TableScan reads 9,800 of 10,000. The query filters with WHERE event_date = (SELECT MAX(load_date) FROM etl_control). The subquery always returns a single date. What should you change first?
Snowflake does not prune on a predicate that contains a subquery, even when the subquery returns a constant. The predicate has to be rewritten into a form that can be checked against the stored value ranges. Pruning does not depend on warehouse size or on any background job. A clustering key fixes poorly ordered data, but it cannot make a subquery predicate prune.
“Snowflake does not prune micro-partitions based on a predicate with a subquery, even if the subquery results in a constant.”Source: docs.snowflake.com
Checkpoint 7 of 8· Exam question
A query on a 6 TB EVENTS table filters with WHERE TO_VARCHAR(event_ts, 'YYYY-MM-DD') = '2026-09-30'. The table is clustered on event_ts, yet the Query Profile TableScan shows Partitions scanned equal to Partitions total. What is the MOST likely cause and fix?
Correct answer: B — Wrapping the clustered column in a function prevents micro-partition pruning; rewrite the predicate as a range directly on event_ts
- A. Incorrect. The result cache returns stored results for identical queries and has no role in how many micro-partitions a fresh scan reads.
- B. Correct. Pruning compares the predicate to per-partition min/max values of the raw column, so applying TO_VARCHAR to it blocks pruning; a range predicate on event_ts restores it.
- C. Incorrect. Micro-partition min/max metadata is held in the cloud services layer, not warehouse memory, so resizing does not change pruning.
- D. Incorrect. Even a suspended clustering service leaves existing metadata usable; the scan reads everything because the predicate cannot be matched to the metadata, not because reclustering is pending.
6.Timeout parameters for running and queued statements
Two parameters put time limits on a statement. STATEMENT_TIMEOUT_IN_SECONDS cancels a statement that runs too long, which stops a runaway or hung query from burning credits. STATEMENT_QUEUED_TIMEOUT_IN_SECONDS cancels a statement that waits in a warehouse queue too long. A queued statement uses no credits, but a result that arrives too late to matter wastes the credits spent producing it. You can set both parameters on the account, a user, a session or a warehouse. When a warehouse and a session both set one, the lowest non-zero value is enforced. A tighter session value can therefore shorten the warehouse limit, but a looser one cannot extend it.
ALTER ACCOUNT SET STATEMENT_TIMEOUT_IN_SECONDS = <number_of_seconds>;
ALTER USER <username> SET STATEMENT_TIMEOUT_IN_SECONDS = <number_of_seconds>;
ALTER SESSION SET STATEMENT_TIMEOUT_IN_SECONDS = <number_of_seconds>;
ALTER WAREHOUSE <warehouse_name> SET STATEMENT_TIMEOUT_IN_SECONDS = <number_of_seconds>;| Parameter | Default on a warehouse | Effect of 0 |
|---|---|---|
| STATEMENT_TIMEOUT_IN_SECONDS | 172800 | The maximum value (604800) is enforced |
| STATEMENT_QUEUED_TIMEOUT_IN_SECONDS | 0 | Disabled: queued statements are never cancelled for waiting |
Checkpoint 8 of 8· Check yourself
Each morning, dashboard queries on a shared warehouse wait behind a nightly load. Some only start 40 minutes later, after the analysts have closed the page, and they still burn credits when they run. Which setting cancels them before they start?
The time a statement spends waiting in a warehouse queue is limited by STATEMENT_QUEUED_TIMEOUT_IN_SECONDS, which cancels it before it consumes any credits. STATEMENT_TIMEOUT_IN_SECONDS limits a statement that is already running, so it acts only after the late query has started spending credits.
“The parameter that controls the amount of time that a SQL statement stays in the queue is STATEMENT_QUEUED_TIMEOUT_IN_SECONDS.”Source: docs.snowflake.com
Sources
(10)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“It includes a Most Expensive Nodes pane that identifies the operator nodes that are taking the longest to execute.”
↩︎ Steps, operator nodes and the operator tree“The Query Profile allows you to examine which parts of a query are taking the longest to execute.”
↩︎ Key concept“You can programmatically access the performance statistics of the Query Profile by executing the GET_QUERY_OPERATOR_STATS function.”
↩︎ Checkpoint“You can programmatically access the performance statistics of the Query Profile by executing the GET_QUERY_OPERATOR_STATS function.”
↩︎ Checkpoint - 2.
“In the query profile, a parent is shown above its child with a link connecting the two.”
↩︎ Steps, operator nodes and the operator tree“Most queries contain a single step, but some are executed as multiple distinct steps.”
↩︎ Steps, operator nodes and the operator tree“The actual execution order of the operations in the plan does not necessarily match the logical order shown by the plan.”
↩︎ EXPLAIN, the execution path and compile-time vs runtime optimization“Runtime optimizations such as join pruning can reduce the number of partitions and bytes scanned during query execution.”
↩︎ EXPLAIN, the execution path and compile-time vs runtime optimization“the number of partitions that might be scanned by the query.”
↩︎ EXPLAIN, the execution path and compile-time vs runtime optimization“Runtime optimizations such as join pruning can reduce the number of partitions and bytes scanned during query execution.”
↩︎ Exam trap 1“EXPLAIN compiles the SQL statement, but does not execute it, so EXPLAIN does not require a running warehouse.”
↩︎ Prediction - 3.
“Special filtering operation that removes tuples that can be identified as not possibly matching the condition of a Join further in the query plan.”
↩︎ EXPLAIN, the execution path and compile-time vs runtime optimization“the Join operator produces significantly (often by orders of magnitude) more tuples than it consumes.”
↩︎ Joins, grouping and sorting that the profile flags“or providing a condition where records from one table match multiple records from another table.”
↩︎ Joins, grouping and sorting that the profile flags“for joins which use equality-based conditions, it lists the expressions used for joining elements.”
↩︎ Joins, grouping and sorting that the profile flags“Non-equality join predicates might result in significantly slower processing speeds and should be avoided if possible.”
↩︎ Joins, grouping and sorting that the profile flags“A common mistake is to use UNION when the UNION ALL semantics are sufficient.”
↩︎ Joins, grouping and sorting that the profile flags“If the local disk space is not sufficient, the spilled data is then saved to remote disks.”
↩︎ Data spilling: local, then remote“The efficiency of pruning can be observed by comparing Partitions scanned and Partitions total statistics in the TableScan operators.”
↩︎ When partition pruning or caching is not helping a scan“the data storage order needs to be correlated with the query filter attributes.”
↩︎ When partition pruning or caching is not helping a scan“A query that reuses the result of a previous query.”
↩︎ When partition pruning or caching is not helping a scan“A query whose result is computed based purely on metadata, without accessing any data. These queries are not processed by a virtual warehouse.”
↩︎ When partition pruning or caching is not helping a scan - 4.
“Performance degrades drastically when a warehouse runs out of memory while executing a query”
↩︎ Data spilling: local, then remote“Using a larger warehouse (effectively increasing the available memory/local storage space for the operation)”
↩︎ Data spilling: local, then remote“Processing data in smaller batches.”
↩︎ Data spilling: local, then remote“You can use the Query Profile to identify which operation nodes are causing data to spill to storage.”
↩︎ Data spilling: local, then remote“Snowflake writes a small amount of data to remote storage for each eligible query, even if QAS isn’t used for that query.”
↩︎ Exam trap 3 - 5.
“Not all predicate expressions can be used to prune.”
↩︎ When partition pruning or caching is not helping a scan“The range of values for each of the columns in the micro-partition.”
↩︎ When partition pruning or caching is not helping a scan“clustering metadata is collected and recorded for each micro-partition created during the process.”
↩︎ When partition pruning or caching is not helping a scan“The smaller the average depth, the better clustered the table is with regards to the specified columns.”
↩︎ When partition pruning or caching is not helping a scan“The micro-partition metadata maintained by Snowflake enables precise pruning of columns in micro-partitions at query run-time”
↩︎ Checkpoint“Snowflake does not prune micro-partitions based on a predicate with a subquery, even if the subquery results in a constant.”
↩︎ Checkpoint - 6.
“Snowflake bypasses query execution and, instead, retrieves the result directly from the cache.”
↩︎ When partition pruning or caching is not helping a scan - 7.
“select the table scan operator and check the Percentage scanned from cache under Statistics.”
↩︎ When partition pruning or caching is not helping a scan - 8.
“the cache is dropped when the warehouse is suspended”
↩︎ When partition pruning or caching is not helping a scan“the cache is dropped when the warehouse is suspended”
↩︎ Exam trap 4 - 9.
“SQL statements that are in a queue to use a warehouse do not consume credits.”
↩︎ Timeout parameters for running and queued statements“When the parameter is set for a warehouse in addition to the session, the lowest non-zero value is enforced.”
↩︎ Exam trap 2“The parameter that controls the amount of time that a SQL statement stays in the queue is STATEMENT_QUEUED_TIMEOUT_IN_SECONDS.”
↩︎ Checkpoint - 10.
“statements will automatically be canceled if they are queued on a warehouse for longer than this amount of time; disabled if set to zero.”
↩︎ Timeout parameters for running and queued statements