BigQuery cost query · Where the money goes
Daily BigQuery cost trend and spike detection
This query returns BigQuery compute per day for the last 90 days (bytes billed, on-demand cost estimate, slot-hours) and a variation flags days that cost more than twice the previous week's average. Run it to see when a cost jump started before you go looking for the job that caused it.
The query
SELECT
DATE(creation_time) AS day,
COUNT(*) AS queries,
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
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY day
ORDER BY day;How it works
A daily series is how you tell a one-off from a new normal. A spike that lasts one day is usually a backfill or an analyst's exploration; a step up that stays is a new pipeline, a dashboard with an aggressive refresh, or a query that lost its partition filter.
Once you have the date, filter the most expensive queries to that day to find the job.
Output columns
| Column | Meaning |
|---|---|
day | UTC date the jobs were created. |
est_on_demand_usd | On-demand list price of the day's bytes billed. |
slot_hours | Slot time used that day. |
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.- Days are UTC. For local days, use
DATE(creation_time, 'Europe/Prague')with your time zone.
Variations
Flag days above twice the previous week's average
WITH daily AS (
SELECT
DATE(creation_time) AS day,
SUM(total_bytes_billed) / POW(1024, 4) * 6.25 AS est_on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY day
),
scored AS (
SELECT
day,
est_on_demand_usd,
AVG(est_on_demand_usd) OVER (ORDER BY day ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING) AS prev_7d_avg
FROM daily
)
SELECT day, ROUND(est_on_demand_usd, 2) AS est_on_demand_usd, ROUND(prev_7d_avg, 2) AS prev_7d_avg
FROM scored
WHERE est_on_demand_usd > 2 * prev_7d_avg
ORDER BY day;FAQ
How do I get alerted on a BigQuery cost spike?
Cloud Billing budgets alert on the billed total, with a delay. For earlier, per-job signals, schedule this query and alert on its rows, or use a tool that watches jobs as they run.
Why do weekends look cheaper?
Ad hoc and BI traffic drops on weekends while scheduled pipelines keep running, so the weekend level shows roughly what your pipelines cost on their own.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
