BigQuery error · Resources and limits
Resources exceeded during query execution
One stage of the query needed more memory than a single BigQuery worker has. The message names the culprit: usually a global ORDER BY, a huge ARRAY_AGG or STRING_AGG group, a skewed JOIN or GROUP BY key, or a window function over one giant partition. Remove or limit that one operation.
The error message
Resources exceeded during query execution: The query could not be executed in the allotted memory. Peak usage: <N>% of limit. Top memory consumer(s): <operation>: <N>%
Causes
ORDER BY over the whole result without LIMIT
A final ORDER BY has to bring every row to one worker to sort it. With a LIMIT, each worker keeps only its own top rows, so memory stays small. Sorting millions of rows for an export or a destination table is the most common trigger.
Aggregating too much into one row
ARRAY_AGG, STRING_AGG and ARRAY_CONCAT_AGG build the whole array or string for a group in memory. One group with millions of values, for example every event of a bot user, is enough to fail the query.
Skewed JOIN or GROUP BY keys
Rows with the same key go to the same worker. If one key, very often NULL, an empty string or a default ID such as 0 or 'unknown', carries a large share of the table, that worker runs out of memory while the others idle.
Window functions over a single huge partition
ROW_NUMBER() OVER (ORDER BY ts) without PARTITION BY, or partitioned by a column with a handful of values, sorts the whole window on one worker. Deduplication written this way is a classic cause.
How to fix it
1. Add LIMIT to the ORDER BY, or drop it
If you only need the top rows, add LIMIT. If you write to a table, don't sort at all: tables have no row order, so the sort is wasted work.
-- Fails on a large table:
-- SELECT * FROM `project.dataset.events` ORDER BY event_ts DESC;
SELECT *
FROM `project.dataset.events`
ORDER BY event_ts DESC
LIMIT 1000;2. Partition the window function
Deduplicate per key instead of numbering the whole table. QUALIFY keeps the latest row per key without a subquery.
SELECT *
FROM `project.dataset.customers`
WHERE TRUE
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1;3. Take the skewed key out of the join
Find the heavy key with a quick count, then filter it out or handle it in a separate query. NULL keys never match in an inner join anyway.
SELECT customer_id, COUNT(*) AS n
FROM `project.dataset.orders`
GROUP BY customer_id
ORDER BY n DESC
LIMIT 10;4. Cap array sizes
ARRAY_AGG and STRING_AGG accept ORDER BY and LIMIT inside the call. Keep only what you need, for example the latest value per group.
SELECT
user_id,
ARRAY_AGG(page ORDER BY event_ts DESC LIMIT 1)[OFFSET(0)] AS last_page
FROM `project.dataset.page_views`
GROUP BY user_id;5. Pre-aggregate or split the query
Aggregate the big table to the grain you join on before the join, or write an intermediate result to a table and run the rest of the query on it. Smaller inputs per stage need less memory per worker.
How to prevent it
- Open the query's Execution details and look for stages where the maximum compute time is far above the average: that gap means skew.
- Track failed jobs with
error_result.reason = 'resourcesExceeded'in INFORMATION_SCHEMA.JOBS so you see the query before the scheduled pipeline fails again. - Buying more slots rarely helps: the limit is the memory of one worker, not your total capacity.
FAQ
Am I charged for a query that fails with resources exceeded?
With on-demand pricing, no: BigQuery doesn't charge for queries that return an error. With capacity (editions) pricing, the slot time the query used before it failed still counts as slot usage in your reservation.
Will more slots or a bigger reservation fix it?
Usually not. The error means a single worker ran out of memory for one operation. More slots add more workers, but the skewed key or the global sort still lands on one of them. Change the query instead.
How do I find every query that hit this error?
Query region-us.INFORMATION_SCHEMA.JOBS for jobs where error_result.reason = 'resourcesExceeded' in the last days, grouped by query or by user_email. Use your own region qualifier.
Related
SQL reference
More resources and limits
All BigQuery error messages · What 50M daily events say about BigQuery failures
