Read your own job history

BigQuery keeps a record of every query you run: the bytes it processed, the bytes it was billed for and the tables it read. You can read those records with SQL, so you see what a query cost instead of guessing.

How it works

A job is one piece of work BigQuery does for you. Each query is a job. A script is a job too, and each statement of it that runs a query is a child job. The JOBS_BY_USER view has one row for each job you ran. A child job carries the id of its script in parent_job_id, and inside a script @@script.job_id is that same id. A statement can therefore list the child jobs that finished before it. The statement that reads the view is not one of the child jobs it lists. BigQuery can take a moment to list a job that finished a second ago, so on a real run the newest child can be missing from the list.

Common mistake

Without WHERE parent_job_id = @@script.job_id the last statement lists every job you have run, and the script's own row comes with them. Filtering on job_id = @@script.job_id goes wrong the other way: it finds only the script's row, with NULL bytes, and none of its queries.

Worked example

SELECT COUNT(*) AS customers FROM learn.shop.customers;
SELECT state, COUNT(*) AS customers FROM learn.shop.customers GROUP BY state;

SELECT statement_type, total_bytes_processed, total_bytes_billed
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_USER
WHERE parent_job_id = @@script.job_id
ORDER BY creation_time;

The two queries above the last statement are its child jobs, listed in the order they ran. The last statement is not one of them.

The count processes 0 bytes and is billed 0. The grouped read processes 80 bytes, the state column, and is billed the 10 MiB minimum.

All BigQuery lessons