BigQuery cost query · Cost attribution
BigQuery cost per dbt model
This query attributes BigQuery compute to dbt models by reading the JSON comment dbt prepends to every query, then ranks models by on-demand cost, slot-hours and run time over the last 30 days. It finds the handful of models worth making incremental or rescheduling.
The query
SELECT
JSON_VALUE(REGEXP_EXTRACT(query, r'/\*\s*(\{"app":\s*"dbt".*?\})\s*\*/'), '$.node_id') AS dbt_node,
COUNT(*) AS jobs,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ROUND(SUM(total_slot_ms) / 3600000, 2) AS slot_hours,
ROUND(AVG(TIMESTAMP_DIFF(end_time, start_time, SECOND))) AS avg_seconds
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
AND REGEXP_CONTAINS(query, r'"app":\s*"dbt"')
GROUP BY dbt_node
ORDER BY est_on_demand_usd DESC;How it works
By default dbt adds a comment to the start of every query it runs: /* {"app": "dbt", "dbt_version": ..., "node_id": "model.my_project.orders"} */. The query extracts that JSON with REGEXP_EXTRACT and reads node_id with JSON_VALUE, so every job is attributed to the model, test or snapshot that ran it.
One model can run several jobs per invocation: an incremental model with the merge strategy creates a temporary table and then runs a MERGE. They all carry the same node_id, so the totals per model are complete.
Output columns
| Column | Meaning |
|---|---|
dbt_node | dbt node ID, for example model.analytics.fct_orders or test.analytics.not_null_orders_id. |
jobs | BigQuery jobs the node ran in the window. |
avg_seconds | Average job duration. |
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). - If your project changes the
query-commentsetting indbt_project.yml, adjust the pattern. Withquery-comment: {job-label: true}, dbt-bigquery also copies the comment's fields into job labels, which you can read like any other label. - Tests count too: a
uniquetest on a large table scans the column every run.
Variations
Cost per dbt invocation target (prod vs dev)
SELECT
JSON_VALUE(REGEXP_EXTRACT(query, r'/\*\s*(\{"app":\s*"dbt".*?\})\s*\*/'), '$.target_name') AS dbt_target,
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'
AND REGEXP_CONTAINS(query, r'"app":\s*"dbt"')
GROUP BY dbt_target
ORDER BY est_on_demand_usd DESC;FAQ
Why is a model I run once a day so expensive?
Usually because it rebuilds the whole table. Making it incremental with a partition filter on the source, or using insert_overwrite on recent partitions, scans only new data.
Does this work for Dataform?
Dataform labels its jobs instead of commenting them; group by those labels with the cost by label query.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
