Snowflake
Snowflake cost per user and per query, with SQL
Snowflake bills warehouses by the second they run, not queries. QUERY_ATTRIBUTION_HISTORY in SNOWFLAKE.ACCOUNT_USAGE is Snowflake's own split of those credits across the queries that ran on each warehouse, so cost per user and cost per query are a GROUP BY away.
Reading ACCOUNT_USAGE needs ACCOUNTADMIN or a role granted the SNOWFLAKE database role USAGE_VIEWER (and GOVERNANCE_VIEWER for QUERY_HISTORY).
Credits by user
credits_attributed_compute is the compute credits Snowflake attributed to each query. Summed by user_name, it is what each user's queries cost.
-- Credits by user, the last 30 days. SELECT user_name, COUNT(*) AS queries, ROUND(SUM(credits_attributed_compute), 2) AS credits, ROUND(SUM(credits_attributed_compute) * 3, 2) AS list_usd FROM snowflake.account_usage.query_attribution_history WHERE start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP()) GROUP BY user_name ORDER BY credits DESC
6.25 is BigQuery's on-demand list price per TiB and 3 is the list price per Snowflake credit that dwhat assumes by default. Put the rate on your contract in their place. Change region-us to the region your jobs run in.
Documented in: Snowflake: QUERY_ATTRIBUTION_HISTORY view · Snowflake: attributing cost
Credits by query
query_parameterized_hash is the same for queries that differ only in their literal values, so a dashboard tile that runs every few minutes adds up to one row. QUERY_HISTORY supplies the text.
-- The 20 most expensive queries, the same query with different values counted as one. SELECT a.query_parameterized_hash, ANY_VALUE(LEFT(q.query_text, 2000)) AS example_query, COUNT(*) AS runs, COUNT(DISTINCT a.user_name) AS users, ANY_VALUE(a.warehouse_name) AS a_warehouse, ROUND(SUM(a.credits_attributed_compute), 2) AS credits, ROUND(SUM(a.credits_attributed_compute) * 3, 2) AS list_usd FROM snowflake.account_usage.query_attribution_history a LEFT JOIN snowflake.account_usage.query_history q ON q.query_id = a.query_id AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP()) WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP()) GROUP BY a.query_parameterized_hash ORDER BY credits DESC LIMIT 20
Documented in: Snowflake: QUERY_ATTRIBUTION_HISTORY view · Snowflake: QUERY_HISTORY view
The credits no query is charged for
Attribution leaves out the time a warehouse runs with nothing to do — waiting out its auto-suspend, or kept up by a schedule — and very short queries. Those credits are still on the invoice. WAREHOUSE_METERING_HISTORY has what each warehouse actually metered; the difference is the part no user or query will ever show.
-- What each warehouse metered against what its queries were attributed, by UTC day.
-- The difference is mostly time the warehouse was running with nothing to do.
WITH metered AS (
SELECT
TO_DATE(CONVERT_TIMEZONE('UTC', start_time)) AS day,
warehouse_name,
SUM(credits_used_compute) AS credits
FROM snowflake.account_usage.warehouse_metering_history
WHERE start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
GROUP BY 1, 2
),
attributed AS (
SELECT
TO_DATE(CONVERT_TIMEZONE('UTC', start_time)) AS day,
warehouse_name,
SUM(credits_attributed_compute) AS credits
FROM snowflake.account_usage.query_attribution_history
WHERE start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
GROUP BY 1, 2
)
SELECT
m.day,
m.warehouse_name,
ROUND(m.credits, 2) AS metered_credits,
ROUND(COALESCE(a.credits, 0), 2) AS attributed_credits,
ROUND(m.credits - COALESCE(a.credits, 0), 2) AS idle_credits
FROM metered m
LEFT JOIN attributed a
ON a.day = m.day AND a.warehouse_name = m.warehouse_name
ORDER BY idle_credits DESC
LIMIT 50QUERY_ATTRIBUTION_HISTORY lags by up to eight hours, so the most recent day looks idler than it was. Cloud services and serverless features are billed separately and are in neither view.
Reconcile it in your browser: export both views as CSV and see the difference by day and warehouse, split into idle time and queries missing from the export.
Documented in: Snowflake: WAREHOUSE_METERING_HISTORY view · Snowflake: QUERY_ATTRIBUTION_HISTORY view
How the auto-suspend delay shapes idle credits
A warehouse is billed per second while it runs, with a 60-second minimum each time it starts or resumes. After its last query it runs idle until AUTO_SUSPEND seconds have passed, so a long delay bills the idle gaps between queries; a short one suspends in them, and each resume is billed at least a minute.
Which way that goes depends on how long the gaps are, and a suspended warehouse also drops its cache.
Compare two auto-suspend settings: describe a repeating pattern of query bursts and see the billed seconds, credits and timeline at each.
Documented in: Snowflake: virtual warehouse credit usage · Snowflake: warehouse considerations
Estimate a warehouse size or resize before it runs
Gen1 standard warehouse credit rates double at each size step. A larger size only saves compute credits if its runtime falls below the inverse rate ratio; speed is an assumption to test.
Estimate warehouse cost by size and hours: compare monthly compute and the runtime factor needed to break even on a resize.
What this misses
- A user is often a service account: the BI tool or the dbt job. Which dashboard or model it was is in the query text or the QUERY_TAG, not in user_name.
- The eight-hour lag means today's numbers keep changing for most of a day.
- QUERY_ATTRIBUTION_HISTORY excludes Adaptive Warehouses, so these attribution queries do not cover them.
- Nobody is told when a user's cost doubles. You find out the next time you run this.
Run this every day instead
dwhat reads the same history with a read-only account, every day. It keeps the daily cost by user, dbt model and dashboard, emails you when a day costs well above its 14-day median and says what grew, and lists the 20 most expensive queries with the fixes above where the evidence supports them.