You are optimizing a BigQuery query that scans 1 TB of data every day. The query joins a large fact table (partitioned by date) with a small dimension table. You notice that the query always scans the entire fact table, even though you only need the last 7 days of data. Which optimization will MOST reduce the bytes scanned?
Trap 1: Create a materialized view that pre-aggregates the data by day.
A materialised view pre-aggregates results but does not prune partitions; the query still reads the full fact table unless a date filter is applied. It is tempting for repeated aggregate queries, where it would be correct, but here the requirement is restricting scanned partitions to the last seven days.
Trap 2: Cluster the fact table on the join key used in the query.
Clustering on the join key speeds up join processing but does not reduce bytes scanned, since clustering prunes only on the clustered columns and the query still reads every date partition. It is tempting for join-heavy queries, where it would be correct, but partition pruning on the date column is what limits scanned bytes.
Trap 3: Change the table to use time-unit column partitioning with a 1-day…
The fact table is already partitioned by date, so redefining partitioning as a time-unit column with a 1-day interval changes nothing about pruning. It is tempting because day-level partitioning is the right pattern for date-filtered scans, but the existing setup already satisfies it; the query needs a date predicate.
- A
Create a materialized view that pre-aggregates the data by day.
Why it fails: A materialised view pre-aggregates results but does not prune partitions; the query still reads the full fact table unless a date filter is applied. It is tempting for repeated aggregate queries, where it would be correct, but here the requirement is restricting scanned partitions to the last seven days.
- B
Add a WHERE clause that filters on the date column used for partitioning.
Partition pruning only activates when the query filters on the partitioning column. Adding that WHERE predicate lets BigQuery eliminate all but the last seven date partitions, cutting bytes scanned from 1 TB to roughly 7 days' worth.
- C
Cluster the fact table on the join key used in the query.
Why it fails: Clustering on the join key speeds up join processing but does not reduce bytes scanned, since clustering prunes only on the clustered columns and the query still reads every date partition. It is tempting for join-heavy queries, where it would be correct, but partition pruning on the date column is what limits scanned bytes.
- D
Change the table to use time-unit column partitioning with a 1-day partition interval.
Why it fails: The fact table is already partitioned by date, so redefining partitioning as a time-unit column with a 1-day interval changes nothing about pruning. It is tempting because day-level partitioning is the right pattern for date-filtered scans, but the existing setup already satisfies it; the query needs a date predicate.