BigQuery cost query · Where the money goes
Which BigQuery tables drive query cost
This query ranks tables by the bytes billed of the queries that read them over the last 30 days, with an even split for queries that read several tables. It points to the tables where partitioning, clustering or a summary table would save the most.
The query
SELECT
CONCAT(t.project_id, '.', t.dataset_id, '.', t.table_id) AS table_ref,
COUNT(DISTINCT j.job_id) AS queries,
ROUND(SUM(j.total_bytes_billed / ARRAY_LENGTH(j.referenced_tables))
/ POW(1024, 4) * 6.25, 2) AS est_on_demand_usd_share
FROM `region-us`.INFORMATION_SCHEMA.JOBS AS j,
UNNEST(j.referenced_tables) AS t
WHERE j.creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND j.job_type = 'QUERY'
AND j.statement_type != 'SCRIPT'
GROUP BY table_ref
ORDER BY est_on_demand_usd_share DESC
LIMIT 50;How it works
referenced_tables lists the tables a job read. JOBS doesn't record bytes per table, so the query splits each job's bytes evenly across the tables it read. The ranking is reliable; the dollar figure per table is an approximation, because a join of a 10 TiB table with a small lookup table puts half the cost on the lookup table.
Tables at the top of this list are where the best fixes pay off: partition or cluster them on the columns queries filter by, require a partition filter, or build a smaller summary table for the dashboards that read them.
Output columns
| Column | Meaning |
|---|---|
table_ref | project.dataset.table read by the queries. |
queries | Distinct query jobs that read the table. |
est_on_demand_usd_share | The table's even share of those jobs' on-demand cost. |
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). - Reads through the Storage Read API (Spark, Dataflow, some BI connectors) are not query jobs and don't appear here.
- Tables read through a view are listed under the view's underlying tables;
referenced_logical_viewsholds the views themselves.
Variations
Full job cost per table, for ranking only
Counts each job's full bytes against every table it read. The totals overlap, but the order is a fair ranking of where the scans go.
SELECT
CONCAT(t.project_id, '.', t.dataset_id, '.', t.table_id) AS table_ref,
ROUND(SUM(j.total_bytes_billed) / POW(1024, 4), 2) AS tib_billed_by_readers
FROM `region-us`.INFORMATION_SCHEMA.JOBS AS j,
UNNEST(j.referenced_tables) AS t
WHERE j.creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND j.job_type = 'QUERY'
AND j.statement_type != 'SCRIPT'
GROUP BY table_ref
ORDER BY tib_billed_by_readers DESC
LIMIT 50;FAQ
Can BigQuery tell me exactly how many bytes each table contributed?
Not from INFORMATION_SCHEMA.JOBS, which only records the total per job. The query plan in Execution details shows bytes read per stage for a single job.
What is the fastest fix for an expensive table?
If queries filter on a date, partition by it and set require_partition_filter. If they filter on a few other columns, cluster by them. Both cut bytes billed without changing the queries.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
