BigQuery cost query · Waste
Find repeated BigQuery queries and cache misses
This query finds queries that run over and over with the same shape, with how often they ran, how often the result cache answered them and what they cost. Repeated queries are the best candidates for materialized views, summary tables, BI Engine or simply a slower refresh.
The query
SELECT
query_info.query_hashes.normalized_literals AS query_hash,
COUNT(*) AS runs,
COUNTIF(cache_hit) AS cache_hits,
COUNT(DISTINCT user_email) AS principals,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ANY_VALUE(LEFT(query, 200)) AS example_query
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY query_hash
HAVING runs >= 100
ORDER BY est_on_demand_usd DESC
LIMIT 50;How it works
query_info.query_hashes.normalized_literals is a hash of the query text that ignores comments, parameter values and literals, so a dashboard query run with different date filters, or a scheduled query run every hour, groups into one row.
Cached results are free, and a query that is identical to a recent one is served from the cache unless something prevents it: non-deterministic functions such as CURRENT_TIMESTAMP() or CURRENT_DATE(), changes to the underlying tables, row-level security, or a destination table. A repeated query with few cache hits and a high cost is usually one of those.
Output columns
| Column | Meaning |
|---|---|
runs | How many times the query shape ran in the window. |
cache_hits | Runs answered from the result cache, which are free. |
est_on_demand_usd | On-demand list price of the bytes the runs billed. |
Permissions and notes
Needs: bigquery.jobs.listAll on the project, for example through the BigQuery Resource Viewer role (roles/bigquery.resourceViewer).
- The
6.25is the on-demand list price per TiB in the US and EU multi-regions; see BigQuery pricing for editions and other regions. - A query that filters on
CURRENT_DATE()is never cached; passing the date as a parameter or literal lets identical runs hit the cache.
Variations
Cache hit rate per principal
SELECT
user_email,
COUNT(*) AS queries,
ROUND(COUNTIF(cache_hit) / COUNT(*) * 100, 1) AS cache_hit_pct
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY user_email
HAVING queries >= 100
ORDER BY queries DESC;FAQ
How long does the BigQuery query cache last?
Cached results are kept for about 24 hours and are per user and project; they are invalidated when the underlying tables change.
When does a materialized view help?
When many queries aggregate the same large table the same way. BigQuery keeps the view up to date incrementally and can rewrite matching queries to read it instead of the base table.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
