BigQuery cost query · Waste
Find dead-end tables: written but never read
This query finds tables that jobs wrote to in the last 30 days but no query read, and what building them cost. Dead-end tables waste both the compute that refreshes them and the storage they occupy, so they are often worth more than unused tables.
The query
WITH recent AS (
SELECT job_id, destination_table, referenced_tables, total_bytes_billed, total_slot_ms
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND state = 'DONE'
AND error_result IS NULL
),
writes AS (
SELECT
destination_table.project_id,
destination_table.dataset_id,
destination_table.table_id,
COUNT(*) AS write_jobs,
SUM(total_bytes_billed) AS bytes_billed,
SUM(total_slot_ms) AS slot_ms
FROM recent
WHERE destination_table.table_id IS NOT NULL
AND NOT STARTS_WITH(destination_table.dataset_id, '_')
GROUP BY 1, 2, 3
),
reads AS (
SELECT DISTINCT t.project_id, t.dataset_id, t.table_id
FROM recent, UNNEST(referenced_tables) AS t
)
SELECT
CONCAT(w.project_id, '.', w.dataset_id, '.', w.table_id) AS table_ref,
w.write_jobs,
ROUND(w.bytes_billed / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd_to_build,
ROUND(w.slot_ms / 3600000, 1) AS slot_hours_to_build
FROM writes AS w
LEFT JOIN reads AS r USING (project_id, dataset_id, table_id)
WHERE r.table_id IS NULL
ORDER BY est_on_demand_usd_to_build DESC;How it works
destination_table is where a job wrote its result, and referenced_tables is what jobs read. A table that shows up as a destination but never as a reference in the same window is being produced for nobody. The filter on datasets starting with _ skips the anonymous datasets BigQuery uses for cached query results and script temporary tables.
The cost column is what the jobs that build the table cost in compute; add its storage from storage cost per table. A dbt model or scheduled query nobody reads anymore is the typical find: switch it off and the cost disappears on the next run.
Output columns
| Column | Meaning |
|---|---|
write_jobs | Successful jobs that wrote to the table in the window. |
est_on_demand_usd_to_build | On-demand list price of the jobs that wrote it. |
slot_hours_to_build | Slot time of those jobs, the cost under editions. |
Permissions and notes
Needs: bigquery.jobs.listAll on the project, for example through the BigQuery Resource Viewer role (roles/bigquery.resourceViewer).
- The
6.25is the on-demand list price per TiB in the US and EU multi-regions; see BigQuery pricing for editions and other regions. - Load jobs count as writes and can target the same tables; that is intended. Reads by jobs in other projects, the Storage Read API and exports are not seen here, so confirm with the table's consumers before turning a pipeline off.
- Staging tables that a later step reads and then replaces should show up as read; if they don't, the reading job may run in another project.
FAQ
What is the difference between an unused table and a dead-end table?
An unused table is not read and usually not written either: it only costs storage. A dead-end table is still being written, so it also costs the compute of every refresh.
How long should the window be?
Longer than the slowest consumer's cycle. A monthly report reads its tables once a month, so use at least 35 days for tables that feed one.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
