BigQuery
Find your most expensive BigQuery queries with SQL
Every query BigQuery runs is recorded in INFORMATION_SCHEMA.JOBS_BY_PROJECT for 180 days: who ran it, the SQL, the bytes billed and the slot time. Summing those by query text answers "which queries cost the most" in one statement.
You need a role that can see every user's jobs in the project, such as BigQuery Resource Viewer. With only your own jobs visible, use INFORMATION_SCHEMA.JOBS_BY_USER instead and the answer is about you alone.
The 20 most expensive queries
On on-demand pricing, read on_demand_usd. On capacity pricing (editions), use slot_hours as a measure of work: multiplying it by a slot-hour rate is an allocation estimate, not the reservation bill, which also depends on provisioned capacity. To rank capacity workloads, change ORDER BY to slot_hours DESC.
The filter is the one dwhat uses: finished query jobs that billed something, with a script's parent job left out because the statements inside it are counted on their own.
-- The 20 most expensive queries of the last 30 days, identical text together. SELECT query, COUNT(*) AS runs, COUNT(DISTINCT user_email) AS users, MAX(creation_time) AS last_run, ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed, ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd, ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) AND job_type = 'QUERY' AND state = 'DONE' AND IFNULL(statement_type, '') != 'SCRIPT' AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0) GROUP BY query ORDER BY tib_billed DESC, slot_hours DESC LIMIT 20
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.
See a sample audit: the same ranking for a made-up BigQuery project, with identical queries grouped and a fix hint where the evidence supports one.
Compare on-demand and capacity pricing: enter a month's billed TiB and a slot reservation you propose, and see both modeled costs and where they match.
Documented in: BigQuery: INFORMATION_SCHEMA.JOBS view · BigQuery pricing
Count a dashboard's query as one query
A dashboard tile that filters on today's date writes different SQL every day, so grouping by text splits it into one row per day. BigQuery records a hash of each query with comments and literals ignored, query_info.query_hashes.normalized_literals; group on that instead and those runs add up to one row. Queries without a hash are excluded here; the text-based query above still includes them.
-- The same, with queries that differ only in their literals counted as one. SELECT query_info.query_hashes.normalized_literals AS query_hash, ANY_VALUE(query) AS example_query, COUNT(*) AS runs, COUNT(DISTINCT user_email) AS users, ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed, ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd, ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) AND job_type = 'QUERY' AND state = 'DONE' AND IFNULL(statement_type, '') != 'SCRIPT' AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0) AND query_info.query_hashes.normalized_literals IS NOT NULL GROUP BY query_hash ORDER BY tib_billed DESC, slot_hours DESC LIMIT 20
Documented in: BigQuery: INFORMATION_SCHEMA.JOBS view
And who ran them
The same view by user_email shows whether the money went to a person, a scheduled job's service account or a BI tool's connection.
-- Who spent it: the last 30 days by user or service account. SELECT user_email, COUNT(*) AS queries, ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed, ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd, ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) AND job_type = 'QUERY' AND state = 'DONE' AND IFNULL(statement_type, '') != 'SCRIPT' AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0) GROUP BY user_email ORDER BY tib_billed DESC, slot_hours DESC
What this misses
- One project and one region at a time. JOBS_BY_PROJECT does not see other projects billed to the same account.
- It names the query, not what sent it: a dbt model or a dashboard shows up only as SQL and a service account.
- It is a snapshot. Next month you run it again, and compare by hand.
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.