BigQuery cost query · Slots and billing model
BigQuery slot usage by hour
This query turns INFORMATION_SCHEMA.JOBS_TIMELINE into the average number of slots your jobs used in each hour of the last week. It is the curve you size a reservation against, and it shows when your heavy pipelines actually run.
The query
SELECT
TIMESTAMP_TRUNC(period_start, HOUR) AS hour,
ROUND(SUM(period_slot_ms) / (1000 * 60 * 60), 1) AS avg_slots
FROM `region-us`.INFORMATION_SCHEMA.JOBS_TIMELINE
WHERE job_creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 8 DAY)
AND period_start >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
AND (statement_type != 'SCRIPT' OR statement_type IS NULL)
GROUP BY hour
ORDER BY hour;How it works
JOBS_TIMELINE has one row per job per second of its run, with the slot milliseconds the job consumed in that second (period_slot_ms). Summing them over an hour and dividing by the milliseconds in an hour gives the average number of slots busy in that hour.
The filter on job_creation_time is there for cost: it is the view's partitioning column. It starts a day earlier than the period_start filter so that jobs created before the window but still running in it are counted.
Output columns
| Column | Meaning |
|---|---|
hour | UTC hour. |
avg_slots | Average slots in use over the hour. Short bursts are flattened; see peak slot usage for them. |
Permissions and notes
Needs: bigquery.jobs.listAll on the project, for example through the BigQuery Resource Viewer role (roles/bigquery.resourceViewer).
(statement_type != 'SCRIPT' OR statement_type IS NULL)drops parent rows of multi-statement queries, which repeat their children's slot time, while keeping load and copy jobs, whose statement type is NULL.- Replace
region-uswith your location. Each region has its own INFORMATION_SCHEMA views. - On-demand projects can use up to 2,000 slots and burst above that; under editions the reservation's baseline and autoscaling limits cap the curve.
Variations
Slot usage per reservation
SELECT
TIMESTAMP_TRUNC(period_start, HOUR) AS hour,
IFNULL(reservation_id, 'on-demand') AS reservation,
ROUND(SUM(period_slot_ms) / (1000 * 60 * 60), 1) AS avg_slots
FROM `region-us`.INFORMATION_SCHEMA.JOBS_TIMELINE
WHERE job_creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 8 DAY)
AND period_start >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
AND (statement_type != 'SCRIPT' OR statement_type IS NULL)
GROUP BY hour, reservation
ORDER BY hour, reservation;FAQ
How many slots do I need?
Look at the hourly average for the baseline you want guaranteed and at the peaks for the autoscaling maximum. Slots you buy as baseline or commitments are billed every hour whether jobs use them or not.
Why is the hourly average much lower than what my reservation shows?
A reservation bills autoscaled slots in steps of 50 and, unless fluid scaling is on, for at least a minute each time it scales, while jobs report only the slot time they used. The gap is idle capacity you pay for.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
