BigQuery Top Queries by Complexity
Complexity in BigQuery is measured by slot consumption — the average number of slots a query uses during execution. This query ranks all queries by their approximate slot count, revealing the most compute-intensive operations in your project.
Why This Matters
High-slot queries affect everyone in your project. Under on-demand pricing they compete for your fair-share slot allocation (typically 2,000 slots). Under Editions pricing they directly consume your reserved capacity. Identifying and optimizing complex queries improves performance for all users and reduces costs under both billing models.
How It Works
Approximate slot count is calculated by dividing total_slot_ms by execution time in milliseconds. A query using 1,000 slot-milliseconds over 1 second used approximately 1 slot on average. This metric captures the parallelism and computational weight of each query.
SQL Query
Fill in your details to get a ready-to-run query:
-- Queries ranked by average slot consumption (most compute-heavy first)
DECLARE lookback_days INT64 DEFAULT 14;
WITH jobs AS (
SELECT
user_email,
query,
project_id,
start_time,
end_time,
total_slot_ms,
COALESCE(total_bytes_billed, 0) AS bytes_billed,
TIMESTAMP_DIFF(end_time, start_time, MILLISECOND) AS duration_ms,
ROW_NUMBER() OVER (PARTITION BY job_id ORDER BY end_time DESC) AS rn
FROM `your-project`.`region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL lookback_days DAY)
AND job_type = 'QUERY' AND state = 'DONE' AND total_slot_ms IS NOT NULL
)
SELECT
user_email,
query,
project_id,
start_time,
end_time,
ROUND(SAFE_DIVIDE(total_slot_ms, duration_ms), 0) AS avg_slots,
ROUND(duration_ms / 1000, 1) AS duration_sec,
ROUND(bytes_billed / POW(1024, 4) * 6.25, 2) AS on_demand_cost_usd
FROM jobs
WHERE rn = 1
...Query Explanation
The core formula is SAFE_DIVIDE(total_slot_ms, duration_ms). If a query used 10,000 slot-milliseconds over 100 milliseconds, its approximate slot count is 100 — meaning it used roughly 100 slots concurrently. Higher numbers indicate more parallelism and computational demand.
Key Insights
Queries with >500 slots are considered highly complex and may benefit from query rewriting.
High slot count with low bytes billed suggests compute-heavy operations: complex JOINs, window functions, or DISTINCT on large datasets.
High slot count with high bytes billed indicates both data volume and compute issues — the biggest optimization targets.
Compare slot count to execution time: if slots are high but execution is fast, the query is efficiently parallel but resource-hungry.
Best Practices
- 1
Simplify multi-way JOINs by pre-aggregating or using intermediate tables.
- 2
Replace exact COUNT(DISTINCT ...) with APPROX_COUNT_DISTINCT() when precision is not critical.
- 3
Avoid correlated subqueries — rewrite them as JOINs or window functions.
- 4
Use clustering on JOIN keys to reduce shuffle operations.
Want CloudClerk to find these savings automatically?
Our platform connects to your BigQuery project, runs these analyses automatically, and delivers AI-powered optimisation recommendations — all with your data fully anonymised.
Related Guides
BigQuery Top Queries by Cost
Find the most expensive BigQuery queries by on-demand cost. Rank queries by total bytes billed to identify the biggest cost drivers in your project.
Read guideBigQuery Top Queries by Duration
Find the longest-running BigQuery queries. Identify slow queries that block resources and impact user experience with runtime analysis.
Read guideBigQuery Top Queries by Frequency
Identify the most frequently executed BigQuery queries. Find repeated queries that are candidates for caching, materialized views, or consolidation.
Read guideBigQuery Slot Usage by Minute
Get minute-level BigQuery slot consumption data. Essential for debugging performance issues and understanding burst slot demand.
Read guide