Removed partition filters
BigQuery skips partitions only when the query filters the partitioning column with a constant expression. Without such a filter, or with one it can't use, it reads every partition.
Updated · First published
Why does my BigQuery query scan every partition?#
Because nothing in the query lets BigQuery skip partitions. On a partitioned table, BigQuery scans the partitions that match a filter on the partitioning column and skips the rest, and pruned partitions aren't counted in the bytes the query scans. With no filter on the partitioning column, or one BigQuery can't use (which filters prune), nothing is pruned and the query reads every partition back to the oldest. A pull request (PR) can cause this by deleting the filter or by rewriting it. On-demand queries are charged per TiB of bytes processed, so you pay for the bytes of every partition the query reads.
What does a removed partition filter cost?#
Every run then reads the partitions the filter used to skip, and with on-demand pricing you pay for them. For example, a daily revenue query reads analytics.stg_orders, a table partitioned daily by order_date:
SELECT order_date, SUM(revenue) AS revenue
FROM analytics.stg_orders
WHERE order_date >= '2024-01-01'
GROUP BY order_date
A PR deletes the WHERE line:
SELECT order_date, SUM(revenue) AS revenue
FROM analytics.stg_orders
GROUP BY order_date
A dry run of each version (free, see below) estimates about 0.334 TiB before and 1.546 TiB after: with no filter, the query reads every partition. At $6.25 per TiB, BigQuery's on-demand list price, that's $2.09 → $9.66 per run: +$7.57, or about +$227 a month at 30 runs.
Which filters let BigQuery skip partitions?#
A filter prunes when it compares the partitioning column with a constant expression. For a table partitioned by a DATE column event_date:
| Filter | Skips partitions? |
|---|---|
event_date >= '2024-01-01' |
Yes |
event_date BETWEEN '2024-01-01' AND '2024-03-31' |
Yes |
DATE_TRUNC(event_date, MONTH) = '2024-03-01' |
Yes: DATE_TRUNC with constant arguments is one of the functions that still prune |
event_date >= '2024-01-01' AND country = 'NL' |
Yes: other conditions joined with AND don't stop pruning |
event_date >= '2024-01-01' OR country = 'NL' |
No: with OR, pruning doesn't work |
event_date = (SELECT MAX(load_date) FROM analytics.load_log) |
No: a subquery isn't a constant expression |
event_date >= first_seen_date |
No: a comparison with another column |
FORMAT_DATE('%Y-%m', event_date) = '2024-03' |
No: most other functions of the column need a full table scan |
Functions that prune when their other arguments are constant: DATE_ADD, DATE_SUB, DATE_DIFF, DATE_TRUNC, DATETIME_DIFF, TIMESTAMP_ADD, TIMESTAMP_SUB, TIMESTAMP_DIFF, TIMESTAMP_TRUNC, EXTRACT (with the YEAR part, or DATE on a timestamp), and FORMAT_TIMESTAMP with the %F, %Y-%m-%d or %Y%m%d format; Google's Query partitioned tables page lists them. On an ingestion-time partitioned table, filter the _PARTITIONTIME or _PARTITIONDATE pseudocolumn, and don't combine it with another column in the comparison: _PARTITIONTIME + field1 = … scans every partition. dbt's BigQuery configurations page gives a shorter rule of thumb, "partition pruning only works when partitions are filtered using literal values"; Google's list is the fuller rule.
So a PR doesn't have to delete a partition filter to stop pruning. Rewriting event_date >= '2024-01-01' as a comparison with a subquery or another column, or wrapping the column in a function that doesn't prune, such as FORMAT_DATE, has the same effect.
How do I check whether a change stops partition pruning?#
Dry-run the query before and after the change and compare the bytes. A dry run doesn't use slots and isn't charged, and it returns the bytes the query would process. Run it in the BigQuery console or with bq query --dry_run --nouse_legacy_sql. Google suggests the same check "to verify if partition pruning is supported on your particular query". A filter that no longer prunes shows up as a jump in the PR version's bytes. To see whether the PR version still prunes at all, dry-run it once more with its partition filter deleted: if the two estimates match, it reads every partition.
On a clustered table, a dry run's estimate is an upper bound: BigQuery uses the clustering to skip blocks only when the query runs. So the base version's real cost can be below its estimate, and when a PR removes a filter on a clustering column, the real increase can be larger than the difference between the two estimates.
To find queries that already read too much, sort the jobs in INFORMATION_SCHEMA.JOBS by total_bytes_billed (on-demand): see job history.
How do I stop queries without a partition filter?#
Require a partition filter on the table. Set the require_partition_filter option:
ALTER TABLE analytics.stg_orders SET OPTIONS (require_partition_filter = true);
A query that doesn't filter the partitioning column then fails with "Cannot query over table '…' without a filter that can be used for partition elimination". There must be at least one condition that references only the partitioning column, and a condition joined with OR doesn't count. The requirement also applies to queries on views and materialized views that read the table. The option's limit: it checks that a usable filter exists, not how many partitions the filter keeps. WHERE order_date >= '2000-01-01' passes, and on a table with no partitions before 2000 it still reads every partition.
Require it in dbt. The require_partition_filter model config does the same for a model's table. Other dbt models and tests that select from that model must then filter its partitioning column too, or they fail. See Catch dbt cost increases before merge.
Check the change before it merges. A CI check that dry-runs the base-branch and PR versions of each changed query shows the cost of a removed filter, a filter that can no longer prune, and a filter that still exists but keeps more partitions. You can script it yourself or use DiffCost (next section). Compare BigQuery cost controls puts the options side by side.
Can a pull request check flag a removed partition filter?#
Yes. DiffCost is a GitHub Action. On each PR it dry-runs the base-branch and PR versions of each changed query or dbt model in your BigQuery project, posts the cost change as a PR comment, and compares the two SQL versions to name the likely cause. A removed partition filter shows as Partition filter removed, one of the comment's high-severity structural risks, next to the cost change:
- On a table's partitioning column. When DiffCost can read the metadata of the tables the base version reads (it needs BigQuery credentials, and the table must be a table or materialized view, not a view, an external table or a snapshot), the finding names the table, its size and its partitioning.
- A rewritten filter. When the PR replaces a pruning comparison on the column with another condition on the same column, DiffCost reports nothing if it recognizes the new condition as pruning: the column compared with a value (
=,<,<=,>,>=,BETWEENorIN (…)), bare or insideDATE,CASTto a date or timestamp type,DATETIME_TRUNC, or one of the functions listed above with constant other arguments. DiffCost doesn't check the column's type, so it also accepts, for example,EXTRACT(YEAR FROM …)on aDATETIMEcolumn, which Google's list doesn't cover. The cost change shows the effect. Any other replacement of a pruning comparison is a removal:event_date >= '2024-01-01'→event_date IS NOT NULL, a comparison with a subquery or another column, or a function BigQuery doesn't prune on, such asEXTRACT(MONTH FROM event_date)orFORMAT_DATE. A filter that didn't prune, such as a comparison with a subquery, can be replaced by any condition on the same column without a finding. - When the PR keeps another pruning comparison on the column in the same
WHEREclause, for example when it drops only the upper bound of a date range, the finding says the query reads more partitions (or "more of the table" when the column was judged by its name), not necessarily all of them. - Columns are also judged by name. A removed filter on a column whose name looks like a date or partition column (such as
event_date,created_at,dtor_PARTITIONTIME) is a Partition filter removed finding too, with or without table metadata. Without metadata (no BigQuery credentials, a failed dry run or metadata read, or a view, external table or snapshot), the name is all DiffCost has to go on. - A removed filter on a clustering column (known from table metadata, so only with BigQuery credentials) is a medium-severity Cluster filter removed finding: the dry run can't show what cluster pruning saved, so the real increase can be larger than the estimate. A clustering column whose name looks like a date or partition column gets Partition filter removed instead.
DiffCost doesn't report a filter moved into or out of a CTE or subquery as removed, as long as the PR has it as many times as the base version. It compares only WHERE clauses, so it does report a filter moved into a JOIN … ON, HAVING or QUALIFY clause, or from several UNION branches up to the query that reads them, as removed. That's a limit of DiffCost's comparison; the cost change shows whether BigQuery still prunes. Structural analysis has every rule and its limits, and Structural risks lists every finding.
The PR comment is free and needs no DiffCost account. To block the merge, set a threshold: a limit on how much any one query's or model's per-run cost may rise, in $/run or percent (not the PR's total). Merge blocking needs a paid plan or the free trial of the Team plan (Plans and billing), an API key, a threshold, the DiffCost GitHub App, and diffcost/cost-check as a required status check with the App as its source: see Merge blocking. A PR can edit the workflow that runs DiffCost, for example to skip it, so also protect the workflow itself.
Next steps#
- Quickstart: add DiffCost to a repository with plain SQL files; no DiffCost account needed.
- BigQuery query cost calculator: what a query costs per run and per month, from its bytes.
- Catch dbt cost increases before merge: which dbt changes raise cost, and how to check them.
- Compare BigQuery cost controls: PR checks, byte limits, quotas, budgets and job history.
- How DiffCost works: how DiffCost turns bytes into dollars, and how accurate the numbers are.
- Google's Query partitioned tables: every pruning rule.
Something missing or wrong? Email support@diffcost.dev.