top of page

How to find your most expensive BigQuery queries with INFORMATION_SCHEMA.JOBS and Gemini

2 hours ago
8 min read
Cover showing the title How to find your most expensive BigQuery queries with INFORMATION_SCHEMA.JOBS and Gemini on a teal data skyline. Data Studios original cover

BigQuery already records the cost driver of every query it runs. The INFORMATION_SCHEMA.JOBS view holds near real-time metadata about all jobs in a project, including the bytes each query billed, the slot time it consumed, the user or service account that ran it and the SQL text itself. One query against that view, filtered to the last 30 days and sorted by total_bytes_billed, returns the short list of jobs that account for most of an on-demand bill, and Gemini in BigQuery can help write that query and explain the expensive SQL it surfaces.


The numbers behind the ranking are public. As listed on Google Cloud's BigQuery pricing page on November 29, 2026, on-demand compute costs $6.25 per TiB, the first 1 TiB of query data processed each month is free, and charges are rounded up to the nearest MB with a minimum of 10 MB per query and per table referenced. Queries that return an error or are served from the cache are not charged. Multiplying total_bytes_billed by the per-TiB price therefore gives a direct cost estimate for every on-demand job in the view.


For BI teams the expensive jobs are rarely one-off experiments. They tend to be dashboard refreshes, scheduled queries and service accounts that rerun the same wide scan several times a day, and the JOBS view makes those patterns visible by user, by query text and by label. The worked example below uses a small illustrative job history with the correct ranking and costs computed in Python, so the result Gemini helps produce can be checked line by line.


··········


WHAT THE BIGQUERY INFORMATION_SCHEMA.JOBS VIEW CONTAINS.


Google's documentation describes INFORMATION_SCHEMA.JOBS as a view with near real-time metadata about all BigQuery jobs in the current project, and states that INFORMATION_SCHEMA.JOBS and INFORMATION_SCHEMA.JOBS_BY_PROJECT are synonymous. Queries against it need a region qualifier, written as `region-us`.INFORMATION_SCHEMA.JOBS for jobs in the US multi-region, and the view keeps 180 days of job history. Jobs run in another location appear only when you query that location's qualifier, so a team with datasets in both the US and the EU has to run the analysis once per region.


The view is partitioned by creation_time and clustered by project_id and user_email. A WHERE clause on creation_time keeps the scan of the view itself small, which is the reason every query in the examples below starts with a time filter.


........


Column

Type

Use in a cost review

job_id

STRING

Identifies the job for follow-up

creation_time

TIMESTAMP

Time filter and partition column

user_email

STRING

User or service account that ran the job

job_type

STRING

Keep QUERY jobs only

statement_type

STRING

Exclude SCRIPT parent jobs

query

STRING

SQL text to group and review

total_bytes_billed

INTEGER

Basis of on-demand cost

total_bytes_processed

INTEGER

Bytes read before billing rules

total_slot_ms

INTEGER

Compute used, relevant under editions

cache_hit

BOOLEAN

Cached results are not charged

parent_job_id

STRING

Links script child jobs to the parent

labels

RECORD

Team, dashboard or pipeline tags


........


Reading the view requires the BigQuery User role (roles/bigquery.user) and the BigQuery Resource Viewer role (roles/bigquery.resourceViewer) on the project, which carry the bigquery.jobs.listAll and bigquery.jobs.create permissions. Without listAll, an analyst can see only part of the picture, so the cost review is usually run by a platform owner or delegated with those two roles.


Two caveats from the documentation shape every cost query. A SCRIPT parent job reports summary values that include its child jobs, so summing parents and children together double counts bytes and slot time; filtering out statement_type = 'SCRIPT' keeps only the jobs that did the work. Under capacity pricing, the documentation notes that you are not billed for bytes and total_bytes_billed is informational only, so a team on BigQuery editions should rank by total_slot_ms instead of converting bytes to dollars.


··········


GEMINI IN BIGQUERY FEATURES FOR WRITING AND EXPLAINING THE COST QUERY.


Gemini in BigQuery is part of Gemini for Google Cloud. The documented SQL features include the SQL generation tool, which opens a "Generate SQL with Gemini" dialog, generating SQL from a comment typed in the query editor, and "Explain this query", which appears when you highlight SQL and select the Gemini button. Completion of SQL queries, conversion of comments to SQL and fixing and explaining SQL errors are listed as Preview features. An administrator normally sets up Gemini in BigQuery for the project, and each user needs the Gemini for Google Cloud User role (roles/cloudaicompanion.user).


........


Tool

Role in the task

Documented requirement

INFORMATION_SCHEMA.JOBS

Job history and bytes billed

BigQuery User and Resource Viewer roles

Generate SQL with Gemini

Draft the ranking query

Gemini for Google Cloud User role

Explain this query

Read an expensive query's SQL

Gemini for Google Cloud User role

BigQuery on-demand pricing

Convert bytes to USD

$6.25 per TiB, first 1 TiB free monthly


........


The cost of Gemini in BigQuery itself is set by Gemini for Google Cloud pricing, which our breakdown of Gemini in BigQuery pricing for SQL assistance and data agents covers in detail. For the general workflow of drafting SQL with Gemini and checking how many bytes a draft will scan before running it, see how to write SQL with Gemini in BigQuery and check the bytes it scans.


··········


PROMPTING GEMINI FOR THE BIGQUERY COST RANKING QUERY.


The prompt works best when it names the view, the region, the columns and the exclusions, because Gemini cannot know which region your jobs run in or that script parents must be removed. A comment typed into the query editor is enough for the generate-from-comment feature.


-- Using `region-us`.INFORMATION_SCHEMA.JOBS, list the 20 query jobs from the
-- last 30 days with the highest total_bytes_billed. Keep job_type = 'QUERY'
-- and state = 'DONE', exclude statement_type = 'SCRIPT'. Show job_id,
-- user_email, creation_time, total_bytes_billed, an estimated cost in USD at
-- 6.25 per TiB (1 TiB = POW(1024, 4) bytes) and the first 120 characters of query.

A correct answer looks like the query below. Compare Gemini's draft with it column by column before running it, paying attention to the time filter, the SCRIPT exclusion and the TiB divisor.


SELECT
  job_id,
  user_email,
  creation_time,
  total_bytes_billed,
  ROUND(total_bytes_billed / POW(1024, 4) * 6.25, 2) AS est_cost_usd,
  LEFT(query, 120) AS query_start
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY'
  AND state = 'DONE'
  AND statement_type != 'SCRIPT'
ORDER BY total_bytes_billed DESC
LIMIT 20;

Single jobs show the worst offenders, while repeated queries show where the money goes over a month. A second query groups identical SQL text by a hash so that a dashboard refresh running every hour appears as one line with its run count and total cost.


SELECT
  TO_HEX(MD5(query)) AS query_hash,
  ANY_VALUE(user_email) AS sample_user,
  ANY_VALUE(LEFT(query, 120)) AS query_start,
  COUNT(*) AS runs,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_cost_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY'
  AND state = 'DONE'
  AND statement_type != 'SCRIPT'
GROUP BY query_hash
ORDER BY tib_billed DESC
LIMIT 20;

Grouping on user_email instead of the hash gives the cost by person or service account, and grouping on a label value gives the cost by team or dashboard when the organization tags its jobs consistently.


··········


WORKED EXAMPLE: RANKING AN ILLUSTRATIVE BIGQUERY JOB HISTORY.


The table below is an illustrative extract of nine jobs, written as if returned by the JOBS view and converted to GiB for readability. A dashboard service account runs the same query four times, the fourth run hits the cache, an analyst runs one large ad hoc join and a script with two child queries, and a third user runs a small lookup that is billed at the 10 MB minimum.


........


job_id

user_email

statement_type

Query

parent_job_id

GiB billed

cache_hit

j01

svc-dash

SELECT

Q_A

none

640

false

j02

svc-dash

SELECT

Q_A

none

640

false

j03

svc-dash

SELECT

Q_A

none

640

false

j04

svc-dash

SELECT

Q_A

none

0

true

j05

maria

SELECT

Q_B

none

2,048

false

j06

maria

SCRIPT

script

none

300

false

j07

maria

SELECT

Q_C

j06

200

false

j08

maria

SELECT

Q_D

j06

100

false

j09

tom

SELECT

Q_E

none

0.01 (10 MB minimum)

false


........


Computed in Python at $6.25 per TiB with 1 TiB equal to 1,024 GiB, the single most expensive job is j05 at 2 TiB, or $12.50. Each uncached run of the dashboard query Q_A bills 0.625 TiB, or $3.91. The cached run j04 bills nothing, and the 10 MB minimum on j09 costs about $0.00006, which rounds to zero at two decimals.


........


Query

Runs

TiB billed

Estimated cost (USD)

Q_B

1

2.000

12.50

Q_A

4

1.875

11.72

Q_C

1

0.195

1.22

Q_D

1

0.098

0.61

Q_E

1

0.000

0.00


........


The grouped view changes the conversation. Q_B is the biggest single job, yet Q_A costs almost as much because it runs repeatedly, and Q_A is the easier target: one change to the dashboard's query or refresh schedule cuts every future run. With the SCRIPT parent excluded, maria's jobs total 2.293 TiB ($14.33), svc-dash totals 1.875 TiB ($11.72) and the whole extract comes to 4.168 TiB, or $26.05 before the free tier.


The double-counting caveat shows up in the numbers. If the SCRIPT row j06 is left in, maria's total rises to 2.586 TiB ($16.16) and the extract to 4.461 TiB ($27.88), an overstatement of $1.83 that comes entirely from counting the two child queries twice. If these nine jobs were the only on-demand queries on the account in a month, the first free TiB would bring the expected charge from $26.05 down to $19.80. The same ranking logic was run in SQLite on the illustrative rows and returned the same order and costs as pandas.


··········


CHECKING GEMINI'S BIGQUERY COST QUERY BEFORE ACTING ON IT.


The errors that distort a cost ranking are small and easy to miss in generated SQL. The first is the unit: dividing by 1,000,000,000,000 instead of POW(1024, 4) overstates the TiB count by about 10 percent, and a draft that uses total_bytes_processed instead of total_bytes_billed ignores the 10 MB minimums and the billing rules. A missing statement_type filter brings back the script double counting shown above, and a missing creation_time filter scans the full 180 days of the view instead of the period under review.


A second group of checks concerns scope. The region qualifier must match where the datasets live, so a query against `region-us` says nothing about jobs in `region-eu`. JOBS covers one project; a bill that spans several projects needs the same query in each of them. Under editions or reservations, a dollar estimate built from bytes describes a charge the team does not actually pay, and slot time is the measure to rank. Our comparison of BigQuery on-demand per-TiB pricing and editions slot-hours sets out when each model applies.


Once the expensive queries are known, "Explain this query" helps an analyst read unfamiliar SQL from another team before proposing a change, such as replacing a scan of every column with the columns the dashboard uses or adding a filter on a partitioned date column. The proposed rewrite still needs a dry run or a check of the bytes estimate before it replaces the original, and the decision to change a production dashboard or a scheduled job stays with its owner. Google's cost best practices also list the maximum bytes billed setting for on-demand queries, and administrators who want a project-level guardrail can use the controls described in our article on disabling on-demand billing with reservation override mode.


The query column contains the full SQL text of every job in the project, which can include customer identifiers or literal values typed into filters. Pasting large extracts of that column into a prompt is a data-governance decision, and the Gemini for Google Cloud User role should be granted only to the people who already have access to that history.


··········


BIGQUERY QUERY COSTS FROM INFORMATION_SCHEMA.JOBS: WHAT THE VIEW CAN AND CANNOT TELL YOU.


Google documents the essentials needed for this task: the JOBS view, its 180-day retention, its partitioning on creation_time, the roles required to read it, the SCRIPT double-counting caveat and the on-demand price of $6.25 per TiB with the first 1 TiB free each month, all verified on November 29, 2026. With those facts, a ranking of total_bytes_billed is an accurate guide to on-demand query spend in one project and one region.


What depends on the account is the pricing model and the scope. Teams on editions pay for slot capacity, so the same view should be ranked by total_slot_ms, and organizations with several projects or regions must repeat the analysis in each. Gemini in BigQuery can draft the queries and explain the SQL behind the expensive jobs, while the region, the unit, the SCRIPT filter and the final change to any production query remain checks for a person who owns the bill.


··········


FOLLOW US FOR MORE.


·····


DATA STUDIOS


·····


datastudios.org


bottom of page