guide
guide7 min read

How to Find the Most Expensive Queries in Snowflake (and Fix Them Automatically)

Rank queries by attributed credits, diagnose the cause, and decide which fixes an agent can apply

To find the most expensive queries in Snowflake, query the SNOWFLAKE.ACCOUNT_USAGE.QUERY_ATTRIBUTION_HISTORY view, which records the compute credits attributed to each query, and join it to QUERY_HISTORY for the SQL text, user, and scan statistics. Both views are documented in the Snowflake QUERY_ATTRIBUTION_HISTORY reference and the QUERY_HISTORY reference. Below we walk through the ranking query, how to read the result, and which fixes are safe to automate.

Key Takeaways

  • •Rank queries by CREDITS_ATTRIBUTED_COMPUTE in ACCOUNT_USAGE.QUERY_ATTRIBUTION_HISTORY, not by elapsed time. A slow query on an XS warehouse can cost less than a fast one on an XL.
  • •Group by QUERY_PARAMETERIZED_HASH so a cheap query that runs 50,000 times a day shows up next to the single big one-off.
  • •Most expensive queries fall into a few patterns: full scans with poor partition pruning, spilling to local or remote storage, and repeated identical work.
  • •Warehouse idle time is not attributed to individual queries, so a query ranking never explains the whole bill. Check warehouse metering separately.
  • •Diagnosis is safe to automate with read-only access. Applying fixes should stay behind an approval step.

Step 1: Know which Snowflake views hold query cost data

Snowflake exposes three places to look. QUERY_HISTORY in the ACCOUNT_USAGE schema has the SQL text, warehouse, size, elapsed time, bytes scanned, partitions scanned versus total, and spill metrics. QUERY_ATTRIBUTION_HISTORY adds the credits attributed to each query's compute. WAREHOUSE_METERING_HISTORY gives the credits each warehouse actually consumed per hour, including time when nothing was running.

Two caveats from the Snowflake docs matter here. ACCOUNT_USAGE views have ingestion latency, so a query that ran ten minutes ago may not appear yet, and QUERY_ATTRIBUTION_HISTORY lags longer than QUERY_HISTORY. Attributed credits also cover query compute only, not idle warehouse time, so the sum across queries will be lower than the warehouse's metered total. Check the docs for the current latency and retention figures before you build alerting on them.

Step 2: How to find the most expensive queries in Snowflake with SQL

Start with the top individual queries over the last seven days. Run: SELECT a.query_id, a.warehouse_name, a.credits_attributed_compute, q.user_name, q.warehouse_size, q.total_elapsed_time / 1000 AS elapsed_s, q.bytes_scanned, LEFT(q.query_text, 200) AS query_snippet FROM snowflake.account_usage.query_attribution_history a JOIN snowflake.account_usage.query_history q ON q.query_id = a.query_id WHERE a.start_time >= DATEADD('day', -7, CURRENT_TIMESTAMP()) ORDER BY a.credits_attributed_compute DESC LIMIT 50;

This answers "which single runs cost the most." It misses the more common problem, which is a modest query that runs constantly. To catch that, group by the parameterized hash: SELECT q.query_parameterized_hash, COUNT(*) AS runs, SUM(a.credits_attributed_compute) AS total_credits, AVG(a.credits_attributed_compute) AS avg_credits, ANY_VALUE(LEFT(q.query_text, 200)) AS sample_text FROM snowflake.account_usage.query_attribution_history a JOIN snowflake.account_usage.query_history q ON q.query_id = a.query_id WHERE a.start_time >= DATEADD('day', -7, CURRENT_TIMESTAMP()) GROUP BY 1 ORDER BY total_credits DESC LIMIT 50;

In our experience the second ranking is the one that changes decisions. A dashboard refresh or an orchestrator task that re-runs the same statement every few minutes tends to beat any ad hoc analyst query once you add it up. That is our observation, not a published benchmark, so check it against your own account.

Step 3: Diagnose why each query is expensive

For the top ten rows, pull the diagnostic columns from QUERY_HISTORY and sort each query into one of four buckets.

  • •Poor pruning: PARTITIONS_SCANNED is close to PARTITIONS_TOTAL on a large table. The filter does not line up with how the data is clustered, or the filter wraps the column in a function.
  • •Spilling: BYTES_SPILLED_TO_LOCAL_STORAGE or BYTES_SPILLED_TO_REMOTE_STORAGE is non-zero. The working set did not fit in warehouse memory, so a joins-and-sorts-heavy query is slow and expensive.
  • •Over-sized warehouse: a short query on a Large or bigger warehouse that would finish fine on a Small. Compare WAREHOUSE_SIZE against elapsed time and bytes scanned.
  • •Repeated identical work: high run count and the same hash. The result is recomputed when it could be cached, materialized, or run less often.

Queueing is a fifth signal worth checking. A high QUEUED_OVERLOAD_TIME means the warehouse is saturated, which is a capacity problem rather than a query problem, and rewriting SQL will not fix it.

Step 4: Fix the expensive queries

Match the fix to the bucket. For poor pruning, add or revise a clustering key on the filter column, or rewrite the predicate so it compares the raw column to a constant. For spilling, either rewrite to reduce the intermediate data (filter before joining, drop unused columns, avoid SELECT * through wide joins) or move that one workload to a larger warehouse. For an over-sized warehouse, drop the size by one step and re-measure. For repeated work, materialize the result or lower the refresh frequency.

Each of these has a cost trade-off. Clustering has its own maintenance credit cost. A bigger warehouse can reduce total credits if the query finishes proportionally faster, and increase them if it does not. This is why we recommend making one change at a time and comparing credits per run before and after, using the same hash-grouped query from Step 2.

Step 5: Automate the diagnosis, gate the fix

Running these queries by hand once is useful. Running them weekly, noticing a new regression, and writing the fix is the part that does not scale. Our Cost Agent runs this analysis continuously over read-only connections: it ranks queries by attributed credits, classifies each into the buckets above, and surfaces the evidence with a proposed change. It also reports on idle warehouses, which the query ranking cannot see.

Every Data Workers agent is an MCP server, so you can ask from inside Claude Code or Cursor: "How much are we spending on Snowflake this month?" or "What's causing the warehouse queue to spike?" The Model Context Protocol is the open standard that lets those clients call the agent as a tool. The open-source edition is read-only by default, so it can diagnose but not change anything. Writes, such as altering a warehouse size or a clustering key, come with the Pro edition behind approval workflows and audit trails. Our Connectors Agent handles the Snowflake connection so the Cost Agent does not need its own credentials setup.

We think the right autonomy level for cost fixes is per change type. Resizing a warehouse downward is easy to reverse, so teams often let an agent propose it with one-click approval. Altering a clustering key on a production table has a cost of its own, so we would keep a human in that loop. The Autonomous Data Conductor exposes this as a per-domain autonomy dial, so you set it once per category rather than approving every action.

What this tutorial does not cover

Cloud services credits, serverless features such as automatic clustering and materialized view maintenance, and storage costs are billed separately from warehouse compute and will not show up in a per-query ranking. If your bill is larger than the sum of your top queries suggests, look at idle warehouse time and serverless feature metering first.

Frequently Asked Questions

How do I find the most expensive queries in Snowflake without ACCOUNT_USAGE access? Without access to the SNOWFLAKE database, use the INFORMATION_SCHEMA.QUERY_HISTORY table function, which covers a shorter window and has no per-query credit attribution. You can still rank by elapsed time, bytes scanned, and warehouse size as a rough proxy for cost, but ask your account admin for ACCOUNT_USAGE access if you need credits.

Is long-running the same as expensive in Snowflake? No. Cost depends on warehouse size multiplied by the time the warehouse spends on the query. A 10-minute query on an XS warehouse can cost less than a 1-minute query on a 4XL, and a query that is mostly waiting in the queue consumes no extra compute of its own. That is why we rank by attributed credits rather than duration.

Why don't my query credits add up to my Snowflake bill? Query attribution covers compute used by queries, not warehouse idle time, cloud services overhead beyond the allowance, storage, or serverless features. Compare the sum against WAREHOUSE_METERING_HISTORY to see how much is idle time, and check the Snowflake docs for exactly what each view includes.

Can an AI agent fix expensive Snowflake queries automatically? An agent can diagnose them automatically with read-only access, and propose a specific fix with evidence. Applying the fix is a separate decision. In Data Workers, writes require the Pro edition and an approval step, and you choose how much autonomy each type of change gets.

How often should we review our most expensive queries? We suggest weekly for the hash-grouped ranking, since regressions usually show up as a new hash climbing the list or an existing one doubling in run count. A continuous agent is more useful than a calendar reminder here because the review only matters when something changed.