BigQuery cost query · Where the money goes
BigQuery cost by project across the organization
This query ranks every project in the organization by bytes billed, on-demand cost estimate and slot-hours over the last 30 days, from one INFORMATION_SCHEMA view. It is the starting point for showback: which teams and products spend what.
The query
SELECT
project_id,
COUNT(*) AS queries,
COUNT(DISTINCT user_email) AS principals,
ROUND(SUM(total_bytes_billed) / POW(1024, 4), 2) AS tib_billed,
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_BY_ORGANIZATION
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY project_id
ORDER BY est_on_demand_usd DESC;How it works
JOBS_BY_ORGANIZATION holds the jobs of every project in the organization for the region, so one query covers the whole estate. Jobs are attributed to the project they ran in, which is also the project that pays for on-demand queries, even when the data lives in another project.
Compare est_on_demand_usd with slot_hours per project: projects that scan a lot per slot-hour are candidates for an editions reservation, the others for staying on-demand. On-demand vs editions does that comparison.
Output columns
| Column | Meaning |
|---|---|
principals | Distinct users and service accounts that ran queries in the project. |
est_on_demand_usd | On-demand list price of the bytes billed in the project. |
slot_hours | Slot time used in the project. |
Permissions and notes
Needs: bigquery.jobs.listAll on the organization, for example the BigQuery Resource Viewer role (roles/bigquery.resourceViewer) granted at the organization level.
- 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. - Without organization-level access, run the same query against
JOBSin each project.
FAQ
Which project pays for a query that reads tables in another project?
The project the job runs in. On-demand bytes are billed to the project that runs the query, not to the project that owns the data.
Does this include storage?
No, this is compute only. Storage cost per project comes from TABLE_STORAGE: see storage cost by table.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
