BigQuery · Metadata view
BigQuery INFORMATION_SCHEMA.JOBS
INFORMATION_SCHEMA.JOBS lists every BigQuery job in a project for the last 180 days, with bytes billed, slot milliseconds, errors and query text. Query it to find the most expensive queries, attribute cost by user or label, and turn total_bytes_billed into an on-demand cost estimate.
INFORMATION_SCHEMA.JOBS (the same view as JOBS_BY_PROJECT) has one row per job in the current project and region: queries, loads, extracts and copies, running or finished in the last 180 days. It is the raw material for any BigQuery cost report. total_bytes_billed, total_slot_ms, cache_hit, error_result, labels, reservation_id, referenced_tables and the full query text are all columns. The data behind the view is partitioned by creation_time and clustered by project_id and user_email, so every query against it should filter on creation_time.
On-demand cost is total_bytes_billed / POW(1024, 4) * 6.25 at the US list price of $6.25 per TiB (see BigQuery pricing for other regions). The column already includes the 10 MB minimums and is 0 for cache hits, but it does not subtract the free first TiB of each month. Failed queries are not charged, while a canceled query (error_result.reason = 'stopped') can be, so Google's own billing query keeps only rows where error_result IS NULL OR error_result.reason = 'stopped'. On capacity (editions) pricing the bytes figure is informational: convert total_slot_ms to slot-hours by dividing by 3,600,000 and multiply by your edition's slot-hour rate for an attribution estimate.
Two details inflate naive totals. A multi-statement query appears as a parent row with statement_type = 'SCRIPT' whose bytes and slot time already include all of its child jobs, so exclude SCRIPT rows before you sum. And BigQuery bills jobs by end_time in the PST8PDT time zone, so daily totals grouped by UTC creation_time will not line up exactly with Cloud Billing.
JOBS covers every user in the project and needs the bigquery.jobs.listAll permission (BigQuery Resource Viewer) plus bigquery.jobs.create. JOBS_BY_USER returns only your own jobs and needs just bigquery.jobs.list (BigQuery User). JOBS_BY_ORGANIZATION covers every project in the organization, needs BigQuery Resource Viewer on the organization, and has no query or dml_statistics column. All of them need a region qualifier such as region-us, and the query must run in that location. Region qualifiers are not hierarchical: region-us is the US multi-region and does not include us-central1, so a project with datasets in several locations needs one query per region. See INFORMATION_SCHEMA for the other views.
Syntax
SELECT ... FROM [project_id.]`region-REGION`.INFORMATION_SCHEMA.JOBS[_BY_PROJECT]
SELECT ... FROM [project_id.]`region-REGION`.INFORMATION_SCHEMA.JOBS_BY_USER
SELECT ... FROM [project_id.]`region-REGION`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATIONArguments and return value
| Argument | Description |
|---|---|
project_id | Optional. The project whose jobs you read. Defaults to the project that runs the query. For JOBS_BY_ORGANIZATION it only selects the organization that contains the project. |
REGION | Required. A dataset location in the form region-us, region-eu or region-europe-west3; the query must run in that location. Without a project prefix the qualifier works unquoted. With one, put the project and the region in backticks: an unquoted my-project.region-us is a syntax error. |
total_bytes_billed | INT64 column. Bytes billed for the job under on-demand pricing, after the 10 MB minimums; 0 for cache hits. Informational only on capacity pricing. |
total_bytes_processed | INT64 column. Bytes the job read. This is what a dry run estimates, before minimums are applied. |
total_slot_ms | INT64 column. Slot milliseconds of the last execution, excluding earlier retries. Divide by 3,600,000 for slot-hours. |
creation_time | TIMESTAMP column. The partitioning column of the view: filter on it to limit how much job history you scan. |
statement_type | STRING column, such as SELECT, INSERT, UPDATE, DELETE, MERGE or SCRIPT. SCRIPT rows are parents of multi-statement queries and double count their children. |
error_result | RECORD column with reason and message. NULL for jobs that succeeded. |
cache_hit | BOOL column. TRUE when the results came from the query cache and nothing was billed. |
labels | ARRAY of STRUCT<key STRING, value STRING>. Job labels for cost attribution by team, pipeline or dashboard. |
Returns: One row per job: running jobs plus job history for the past 180 days, for the selected project (or user, or organization) and region.
Examples
Most expensive queries of the last 7 days
Sorts by bytes billed and adds an on-demand cost estimate at $6.25 per TiB and the slot-hours each job consumed. Sort by total_slot_ms instead if the project runs on a reservation.
SELECT
job_id,
user_email,
creation_time,
ROUND(total_bytes_billed / POW(1024, 4), 3) AS tib_billed,
ROUND(total_bytes_billed / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ROUND(total_slot_ms / 3600000, 2) AS slot_hours,
LEFT(query, 200) AS query_start
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
AND (error_result IS NULL OR error_result.reason = 'stopped')
ORDER BY total_bytes_billed DESC
LIMIT 20;Cost by user for the current month
The same estimate grouped by user_email. Service accounts show up here too, which is usually where scheduled pipelines and BI tools hide.
SELECT
user_email,
COUNT(*) AS jobs,
COUNTIF(cache_hit) AS cache_hits,
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_TRUNC(CURRENT_TIMESTAMP(), MONTH)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
AND (error_result IS NULL OR error_result.reason = 'stopped')
GROUP BY user_email
ORDER BY est_on_demand_usd DESC;Failed jobs grouped by error reason
error_result.reason is the short code, such as invalidQuery, resourcesExceeded, accessDenied, notFound, quotaExceeded, bytesBilledLimitExceeded or stopped for canceled jobs. One sample message per reason is usually enough to see what broke.
SELECT
error_result.reason AS reason,
COUNT(*) AS failed_jobs,
COUNT(DISTINCT user_email) AS users,
ANY_VALUE(error_result.message) AS sample_message
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
AND error_result IS NOT NULL
GROUP BY reason
ORDER BY failed_jobs DESC;The cost arithmetic on sample job rows
1 TiB is 1024^4 bytes and a slot-hour is 3,600,000 slot milliseconds. The cached rerun bills nothing. The Enterprise column multiplies slot-hours by the $0.06 pay-as-you-go rate: an attribution estimate, not the reservation invoice.
WITH jobs AS (
SELECT 'nightly_rollup' AS job, 3298534883328 AS total_bytes_billed, 9000000 AS total_slot_ms UNION ALL
SELECT 'adhoc_backfill', 549755813888, 1800000 UNION ALL
SELECT 'cached_rerun', 0, 0
)
SELECT
job,
total_bytes_billed / POW(1024, 4) AS tib_billed,
ROUND(total_bytes_billed / POW(1024, 4) * 6.25, 2) AS on_demand_usd,
total_slot_ms / 3600000 AS slot_hours,
ROUND(total_slot_ms / 3600000 * 0.06, 2) AS enterprise_usd
FROM jobs
ORDER BY total_bytes_billed DESC;| job | tib_billed | on_demand_usd | slot_hours | enterprise_usd |
|---|---|---|---|---|
| nightly_rollup | 3.0 | 18.75 | 2.5 | 0.15 |
| adhoc_backfill | 0.5 | 3.13 | 0.5 | 0.03 |
| cached_rerun | 0.0 | 0.0 | 0.0 | 0.0 |
Common mistakes
- Summing SCRIPT rows together with their child jobs. The parent row of a multi-statement query already includes its children's bytes and slot time, so totals come out roughly double. Add
statement_type != 'SCRIPT'. - Reading the wrong location.
region-usis the US multi-region only; jobs that ran inus-central1,EUoreurope-west3are in their own views, and one query cannot join regions. Querying with the wrong qualifier fails with a Not found error that names the location. - Treating the estimate as the invoice.
total_bytes_billeddoes not subtract the free first TiB per month, failed jobs are not charged, and on capacity pricing you pay for the slots the reservation and autoscaler allocate, not fortotal_slot_ms. Reconcile with Cloud Billing, or withINFORMATION_SCHEMA.RESERVATIONS_TIMELINEfor reservations. - Expecting query text in
JOBS_BY_ORGANIZATION. It has noquerycolumn. Find the expensivejob_idat organization level, then read the text from that project'sJOBSview. - Writing the view name in lowercase.
INFORMATION_SCHEMAview names are case-sensitive, andregion-us.information_schema.jobsis read as an ordinary dataset and table, which fails with an Access Denied error that suggests the table may not exist.
Cost and performance
- On on-demand projects every
INFORMATION_SCHEMAquery bills at least 10 MB and is never cached, so a dashboard that refreshes a JOBS query every few minutes pays each time. Point dashboards at a daily summary table instead. - Filter on
creation_time(the partitioning column) so BigQuery scans only the days you need;project_idanduser_emailare clustering columns. A dry run of a JOBS query can report far more bytes than the query reads, because the estimate only accounts for thecreation_timefilter. - History stops at 180 days. To compare costs year over year, append yesterday's rows to your own table partitioned by
DATE(creation_time)with a scheduledINSERT ... SELECT, filtered to one day per run.
FAQ
How do I find the most expensive queries in BigQuery?
Query region-us.INFORMATION_SCHEMA.JOBS with a creation_time filter, exclude statement_type = 'SCRIPT', and order by total_bytes_billed on on-demand pricing or by total_slot_ms on a reservation. The first example on this page does both.
How do I calculate query cost from INFORMATION_SCHEMA.JOBS?
On-demand: total_bytes_billed / POW(1024, 4) * 6.25 gives US list-price dollars per job. On editions: total_slot_ms / 3600000 gives slot-hours, which you multiply by your edition's slot-hour rate from BigQuery pricing.
What is the difference between JOBS, JOBS_BY_USER and JOBS_BY_ORGANIZATION?
Scope and permissions. JOBS shows every user's jobs in one project (bigquery.jobs.listAll), JOBS_BY_USER only your own (bigquery.jobs.list), and JOBS_BY_ORGANIZATION every project in the organization, without the query and dml_statistics columns.
How do I break BigQuery cost down by label?
Unnest the labels array: FROM region-us.INFORMATION_SCHEMA.JOBS, UNNEST(labels) AS l WHERE l.key = 'team', then group by l.value. Jobs without that label drop out of a comma join, so count them separately.
Related
Errors you may hit
All BigQuery INFORMATION_SCHEMA · All statements and clauses · BigQuery error messages
