BigQuery cost query · Cost attribution
BigQuery cost by label: team, product or pipeline
This query splits BigQuery compute by the value of a job label, such as team or cost center, and shows how much spend has no label at all. Labels are how BigQuery cost gets an owner, so the unlabeled share is the first number to drive down.
The query
SELECT
IFNULL((SELECT value FROM UNNEST(labels) WHERE key = 'team'), '(no team label)') AS team,
COUNT(*) AS queries,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours
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 team
ORDER BY est_on_demand_usd DESC;How it works
labels is an array of key-value pairs attached to each job. The subquery (SELECT value FROM UNNEST(labels) WHERE key = 'team') reads one key; jobs without it fall into (no team label). Change 'team' to whatever key you use: cost_center, product, env.
Labels get onto jobs in a few ways. Client libraries and bq query --label team:finance set them per job. In SQL, SET @@query_label = "team:finance"; labels every following query in the session or script. dbt, Airflow and Looker Studio add their own labels, which the next queries on this page use.
Output columns
| Column | Meaning |
|---|---|
team | Value of the team label, or (no team label). |
est_on_demand_usd | On-demand list price of the label's bytes billed. |
slot_hours | Slot time used, the cost driver under editions. |
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.- Label keys and values are lowercase letters, digits, underscores and dashes, up to 63 characters.
Variations
Every label key and how much spend it covers
SELECT
l.key,
COUNT(DISTINCT j.job_id) AS queries,
ROUND(SUM(j.total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS AS j,
UNNEST(j.labels) AS l
WHERE j.creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND j.job_type = 'QUERY'
AND j.statement_type != 'SCRIPT'
GROUP BY l.key
ORDER BY est_on_demand_usd DESC;Label the queries in a script or session
SET @@query_label = "team:finance,pipeline:revenue_daily";
-- Every query after this line carries both labels.
SELECT COUNT(*) FROM `project.dataset.orders`;FAQ
Do labels on datasets and tables apply to queries?
No. Dataset and table labels show up in billing for storage, but query cost is attributed by job labels. Set labels on the jobs.
How much spend should be labeled?
Aim for every pipeline and BI tool to label its jobs; what remains unlabeled is then ad hoc work, which user_email already attributes.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
