BigQuery cost query · Where the money goes
Find the most expensive queries in BigQuery
This query lists the BigQuery jobs that billed the most bytes in the last 30 days, with an on-demand cost estimate, slot-hours and the start of the SQL. It is the first query to run when the bill jumps: a handful of jobs usually explains most of the spend.
The query
SELECT
job_id,
user_email,
creation_time,
ROUND(total_bytes_billed / POW(1024, 4), 3) AS tib_billed,
ROUND(total_bytes_billed / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ROUND(total_slot_ms / 3600000, 2) AS slot_hours,
LEFT(query, 200) AS query_start
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
ORDER BY total_bytes_billed DESC
LIMIT 50;How it works
Every query job leaves a row in INFORMATION_SCHEMA.JOBS with the bytes it billed (total_bytes_billed) and the slot time it used (total_slot_ms). On-demand pricing charges per TiB billed, so sorting by total_bytes_billed puts the most expensive jobs first. Slot-hours are what the same job would cost under editions.
One expensive job is a cleanup task. The same query run 2,000 times a month is a design problem: group by the normalized query hash (second variation) to find those, because a scheduled dashboard refresh rarely shows up in a top-50 list of single jobs.
Output columns
| Column | Meaning |
|---|---|
tib_billed | TiB billed by the job, after the 10 MB minimum per table and per query. |
est_on_demand_usd | On-demand list price for those bytes. Not your bill if you use editions or have discounts. |
slot_hours | Slot time the job used. Under editions, cost is slot-hours times the price per slot-hour. |
query_start | First 200 characters of the SQL, enough to recognise the job. |
Permissions and notes
Needs: bigquery.jobs.listAll on the project, for example through the BigQuery Resource Viewer role (roles/bigquery.resourceViewer). To see only your own jobs, query JOBS_BY_USER instead.
- The
6.25is the on-demand list price per TiB in the US and EU multi-regions. If your projects run on editions, cost follows slot-hours instead: multiplyslot_hoursby your edition's price per slot-hour (see BigQuery pricing). statement_type != 'SCRIPT'drops the parent row of multi-statement queries. The parent repeats the bytes and slots of its child statements, so keeping it would count them twice.- Replace
region-uswith the location your jobs run in (region-eu,region-europe-west2, ...). Each region has its own INFORMATION_SCHEMA views, so a multi-region estate needs one query per region. - INFORMATION_SCHEMA.JOBS keeps 180 days of history. The view is partitioned by
creation_time, so the time filter also keeps this query cheap.
Variations
Across every project in the organization
SELECT
project_id,
job_id,
user_email,
ROUND(total_bytes_billed / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
LEFT(query, 200) AS query_start
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
ORDER BY total_bytes_billed DESC
LIMIT 50;Group repeated runs of the same query
query_info.query_hashes.normalized_literals is the same for queries that differ only in literals, comments or parameter values, so a scheduled query and its daily runs collapse into one row.
SELECT
query_info.query_hashes.normalized_literals AS query_hash,
COUNT(*) AS runs,
ANY_VALUE(user_email) AS example_user,
ANY_VALUE(LEFT(query, 200)) AS example_query,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY query_hash
ORDER BY est_on_demand_usd DESC
LIMIT 50;FAQ
Why is the estimate different from my invoice?
It applies the on-demand list price to bytes billed. Your invoice also reflects the 1 TiB monthly free tier, discounts, editions or reservations, storage and other services. Use it to rank queries, and Cloud Billing to reconcile totals.
Does INFORMATION_SCHEMA.JOBS cost money to query?
Yes, it is billed like any query, with a 10 MB minimum. Filtering on creation_time, the partitioning column, keeps the scan small.
How do I see queries from other projects?
Use JOBS_BY_ORGANIZATION, which needs bigquery.jobs.listAll at the organization level, or run the query in each project.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
