What you will be able to do
- Find slow, spilling and repeated queries across the account with QUERY_HISTORY
- Assign compute credits to individual queries with QUERY_ATTRIBUTION_HISTORY
- Tell queuing caused by overload apart from time spent waiting for provisioning
- Lay out warehouses so each one runs a similar workload
1.QUERY_HISTORY: every query in the account for a year
The Query Profile explains one query. Finding which queries to look at needs account-wide data. That comes from SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY, one of the shared account usage views (SNOWFLAKE.ACCOUNT_USAGE schema). By default only ACCOUNTADMIN can query it. It keeps 365 days of history with a latency of up to 45 minutes. The Information Schema QUERY_HISTORY table function is updated sooner and needs no ACCOUNT_USAGE access, but it covers only the past 7 days.
| Column | What it measures | Symptom it reveals |
|---|---|---|
| bytes_spilled_to_local_storage | Volume of data spilled to local disk | Memory pressure |
| bytes_spilled_to_remote_storage | Volume of data spilled to remote disk | Severe memory pressure |
| partitions_scanned / partitions_total | Micro-partitions scanned vs total in the query's tables | Ineffective pruning when close to 1 |
| queued_overload_time | Milliseconds queued because the warehouse was overloaded | Too much concurrent work |
| queued_provisioning_time | Milliseconds queued waiting for compute to provision on creation, resume or resize | Startup or resize wait, not overload |
| total_elapsed_time | Elapsed time in milliseconds | Slow queries overall |
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;One false alarm is worth knowing. When the query acceleration service is enabled, Snowflake writes a small amount of data to remote storage for every eligible query, even ones QAS doesn't accelerate. A small nonzero bytes_spilled_to_remote_storage on such a warehouse is therefore not a memory problem.
A query that is cheap per run can still be expensive in total if it runs thousands of times. The query_hash column holds a hash of the canonicalized SQL text, and query_parameterized_hash holds a hash of the parameterized query, so runs that differ only in literal values group together. Grouping by either hash and summing elapsed time shows which repeated queries to optimize first.
SELECT
query_hash,
COUNT(*),
SUM(total_elapsed_time),
ANY_VALUE(query_id)
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE warehouse_name = 'MY_WAREHOUSE'
AND DATE_TRUNC('day', start_time) >= CURRENT_DATE() - 7
GROUP BY query_hash
ORDER BY SUM(total_elapsed_time) DESC
LIMIT 100;Checkpoint 1 of 5· Fill the gap
This query ranks repeated queries on a warehouse by their total elapsed time over the last 7 days. Which view completes the FROM clause?
SELECT
query_hash,
COUNT(*),
SUM(total_elapsed_time),
ANY_VALUE(query_id)
FROM SNOWFLAKE.ACCOUNT_USAGE. ?
WHERE warehouse_name = 'MY_WAREHOUSE'
AND DATE_TRUNC('day', start_time) >= CURRENT_DATE() - 7
GROUP BY query_hash
ORDER BY SUM(total_elapsed_time) DESC
LIMIT 100;QUERY_HISTORY has one row per query, with query_hash and total_elapsed_time. Grouping by the hash shows which repeated queries cost the most time in total. QUERY_INSIGHTS lists detected conditions, not elapsed times, and WAREHOUSE_LOAD_HISTORY measures load per warehouse, not per query.
Source: docs.snowflake.comCheckpoint 2 of 5· Check yourself
A warehouse has the query acceleration service enabled. In ACCOUNT_USAGE.QUERY_HISTORY, many of its queries show a small nonzero bytes_spilled_to_remote_storage. What should you conclude?
When QAS is enabled, Snowflake writes a small amount of data to remote storage for every eligible query, even ones it doesn't accelerate. A small nonzero value here is therefore not a sign that the query ran out of memory.
“don’t be concerned by a nonzero value for bytes_spilled_to_remote_storage in the QUERY_HISTORY view when QAS is enabled.”Source: docs.snowflake.com
2.QUERY_ATTRIBUTION_HISTORY: the credits each query used
QUERY_HISTORY tells you how long a query took. QUERY_ATTRIBUTION_HISTORY tells you what it cost in warehouse compute, over the last 365 days. Its main column is CREDITS_ATTRIBUTED_COMPUTE. When queries run at the same time, the warehouse's cost is split among them by the weighted average of their resource consumption, and any resizing or multi-cluster autoscaling is included. For an accelerated query, the total is that column plus CREDITS_USED_QUERY_ACCELERATION.
Several things are left out of attributed credits, so the per-query totals won't add up to the warehouse's whole bill:
- Warehouse idle time, a period with no queries running that can be measured only at the warehouse level
- Data transfer, storage, cloud services and serverless feature costs, and tokens processed by AI services
- Queries of about 100 ms or less, which are too short to attribute and don't appear in the view
- Jobs run by Adaptive Warehouses, which are reported in
QUERY_METERING_HISTORYinstead
Latency is up to eight hours, much longer than the 45 minutes for QUERY_HISTORY. The view has the same QUERY_TAG, USER_NAME, QUERY_HASH and QUERY_PARAMETERIZED_HASH columns, so costs can be rolled up by user, by tag or by recurring query. For a stored procedure that issues child queries, sum the rows whose root_query_id, or query_id, equals the procedure's root query ID.
SELECT user_name, SUM(credits_attributed_compute) AS credits
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_ATTRIBUTION_HISTORY
WHERE user_name = CURRENT_USER()
AND start_time >= DATE_TRUNC('MONTH', CURRENT_DATE)
AND start_time < CURRENT_DATE
GROUP BY user_name;Checkpoint 3 of 5· Check yourself
For last month, you sum CREDITS_ATTRIBUTED_COMPUTE over every query that ran on one warehouse. The total is clearly lower than the compute credits the warehouse was billed for that month. What explains the gap?
Attributed credits cover only the time queries were executing. Idle time can be measured only at the warehouse level, and very short queries are too short to attribute, so the per-query sum falls below the warehouse bill. Resizing and autoscaling are included, concurrent queries share the cost by weighted average, and an eight-hour latency doesn't affect a month that has already ended.
“Short-running queries (<= ~100ms) are currently too short for per query cost attribution and are not included in the view.”Source: docs.snowflake.com
Sources3
3.Queuing and grouping similar workloads
Queuing is a problem that happens before a query runs. When too many queries reach a warehouse at once, its compute resources run out and new queries wait in line. The Query Profile of a queued query can look perfectly healthy, because the delay comes before execution. The signs are elsewhere: the insight Query was in the queue for the warehouse for too long, the Queued load colour in a warehouse's Warehouse Activity chart in Snowsight, and the queued columns in QUERY_HISTORY.
The queued columns separate two causes. queued_overload_time is time spent waiting because the warehouse was busy with other queries. That is a concurrency problem. queued_provisioning_time is time spent waiting for compute to be provisioned because the warehouse was being created, resumed or resized. That is a startup cost, and adding concurrency won't remove it. A third column, queued_repair_time, counts time spent waiting for compute resources to be repaired.
Overload queuing calls for spreading the load, not making each query faster (Scale up vs scale out). For a standard warehouse, create more warehouses and distribute the queries among them, starting with the queries that cause the spikes. On Enterprise Edition, convert to a multi-cluster warehouse, or raise its maximum cluster count. If a multi-cluster warehouse uses the Economy scaling policy, some queuing is expected, because that policy keeps the running clusters fully loaded before it starts new ones. The queuing insight also suggests a larger warehouse, or one that runs fewer concurrent queries.
SELECT TO_DATE(start_time) AS date,
warehouse_name,
SUM(avg_running) AS sum_running,
SUM(avg_queued_load) AS sum_queued
FROM snowflake.account_usage.warehouse_load_history
WHERE TO_DATE(start_time) >= DATEADD(month,-1,CURRENT_TIMESTAMP())
GROUP BY 1,2
HAVING SUM(avg_queued_load) >0;Each fix above is easier when a warehouse runs one kind of work. Size, concurrency limits and scaling policy all suit some queries and waste money on others. A warehouse that mixes heavy hourly ETL with a latency-sensitive dashboard can't be tuned for both. A resize meant for the ETL is paid for by dashboard queries that didn't need it, and ETL bursts queue the dashboard. Put queries with similar size and concurrency on the same warehouse, so each warehouse has one symptom to tune for. Bucketing a warehouse's queries by execution time in QUERY_HISTORY shows when a group of queries should move to a warehouse of its own.
Checkpoint 4 of 5· Match them up
Match each QUERY_HISTORY queue column to the cause of the wait it records
Tap a term, then the definition that fits it.
Only overload time means too much concurrent work, so only that column calls for spreading the load. Provisioning time comes from creating, resuming or resizing the warehouse, and repair time comes from compute being repaired. Neither is caused by other queries competing for the warehouse.
“waiting for the warehouse compute resources to provision, due to warehouse creation, resume, or resize.”Source: docs.snowflake.com
Checkpoint 5 of 5· Check yourself
Heavy hourly ETL jobs and a latency-sensitive dashboard share one warehouse. During ETL bursts, dashboard queries queue, and a resize to speed up the ETL also raised the cost of the dashboard queries. What is the best-practice fix?
A warehouse is easier to tune when it runs similar workloads. Splitting the work means the ETL resize isn't paid for by dashboard queries that don't need it, and ETL bursts stop queuing the dashboard. Resizing the shared warehouse again keeps the mix. The Economy policy accepts some queuing because it keeps running clusters fully loaded before starting new ones.
“Optimizing a warehouse for query performance is more straightforward when the warehouse runs similar workloads.”Source: docs.snowflake.com
Practise it for real
Use ACCOUNT_USAGE to find the queries in your account most worth tuning, then open one in the Query Profile.
1.With a role that can read the SNOWFLAKE database (ACCOUNTADMIN by default), run the top-10 spill query against SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY.
Why: Remote spilling is the costliest memory symptom, and this query ranks it across 45 days.
You should see: Up to 10 rows with query_id, warehouse_name and the two bytes_spilled columns. An empty result means no spilling in the period.
2.Copy a query_id from the result, then open Monitoring » Query History in Snowsight, select that query ID and open the Query Profile tab.
Why: The account view tells you which query spilled. The profile tells you which operator spilled.
You should see: A node, often an Aggregate, Sort or Join, with Bytes spilled statistics in its pane, and possibly a Remote spillage insight.
3.Run the query_hash query against QUERY_HISTORY for one of your warehouses.
Why: Repeated queries can cost more in total than a single slow one.
You should see: Up to 100 hashes with their run count and summed total_elapsed_time, ordered by total time.
4.Run the per-user QUERY_ATTRIBUTION_HISTORY query.
Why: It shows the compute credits your own queries used this month, excluding idle time.
You should see: One row with your user name and a credits figure. Recent queries may be missing because of the view's latency of up to eight hours.
Stuck? Get a nudge
If the attribution query returns nothing, check whether your queries ran today: the sample filters start_time < CURRENT_DATE.
Sources
(6)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“the table function restricts the results to activity over the past 7 days, versus 365 days for the Account Usage view.”
↩︎ QUERY_HISTORY: every query in the account for a year“Latency for the view may be up to 45 minutes.”
↩︎ QUERY_HISTORY: every query in the account for a year“Time (in milliseconds) spent in the warehouse queue, due to the warehouse being overloaded by the current query workload.”
↩︎ Queuing and grouping similar workloads“waiting for the warehouse compute resources to provision, due to warehouse creation, resume, or resize.”
↩︎ Checkpoint - 2.
“a frequently repeated query could lead to high costs, based on the number of times the query runs.”
↩︎ QUERY_HISTORY: every query in the account for a year“By default, only the account administrator (i.e. user with the ACCOUNTADMIN role) can access views in the ACCOUNT_USAGE schema.”
↩︎ QUERY_HISTORY: every query in the account for a year - 3.
“determine the compute cost of a given query run on warehouses in your account in the last 365 days”
↩︎ QUERY_ATTRIBUTION_HISTORY: the credits each query used“Short-running queries (<= ~100ms) are currently too short for per query cost attribution and are not included in the view.”
↩︎ QUERY_ATTRIBUTION_HISTORY: the credits each query used“Latency for this view can be up to eight hours.”
↩︎ QUERY_ATTRIBUTION_HISTORY: the credits each query used“you can compute the attributed query costs for the procedure by using the root query ID for the procedure.”
↩︎ QUERY_ATTRIBUTION_HISTORY: the credits each query used“Idle time is a period of time in which no queries are running in the warehouse and can be measured at the warehouse level.”
↩︎ Exam trap 2“Includes only the credit usage for the query execution and doesn’t include any warehouse idle time.”
↩︎ Prediction - 4.
“the warehouse’s compute resources become exhausted and subsequent queries are queued until resources become available.”
↩︎ Queuing and grouping similar workloads“consider creating additional warehouses, and then distribute the queries among them. If specific queries are causing usage spikes, focus on moving those queries.”
↩︎ Queuing and grouping similar workloads“This might result in queries being queued and taking longer to complete.”
↩︎ Queuing and grouping similar workloads - 5.
“Optimizing a warehouse for query performance is more straightforward when the warehouse runs similar workloads.”
↩︎ Queuing and grouping similar workloads“the cost of a performance enhancement might be wasted on a query that does not benefit from the optimization.”
↩︎ Queuing and grouping similar workloads
Also cited
“don’t be concerned by a nonzero value for bytes_spilled_to_remote_storage in the QUERY_HISTORY view when QAS is enabled.”
↩︎ Exam trap 1“don’t be concerned by a nonzero value for bytes_spilled_to_remote_storage in the QUERY_HISTORY view when QAS is enabled.”
↩︎ Checkpoint