When should you use partitioning vs clustering in BigQuery, and how do they affect cost and performance?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
When should you use partitioning vs clustering, and how do they impact cost and performance?
Answer for BigQuery, where on-demand queries are billed by the bytes they process.
```hint Look at the columns
Think about how many distinct values each kind of column has, and about when the engine can know how many bytes a query will read before it runs.
```
### What a Strong Answer Covers
- What each feature does to storage: partitions as separate segments the planner can skip, clustering as sorted blocks with value ranges the engine can skip
- Which columns suit each, including cardinality, filter patterns, the number of columns, and column order for clustering
- The effect on cost under bytes-billed pricing, including how predictable the savings are and the option to require a partition filter
- How to combine them, and the cases where they fail to help: too many small partitions, filters that defeat pruning, small tables
### Follow-up Questions
- A dry run says a query will process 500 GB, but the job bills 40 GB. Why?
- When would you cluster a table without partitioning it?
- Your queries filter on the partition column wrapped in a function, and costs did not drop. What happened?
- How does the picture change under capacity (slot-based) pricing?
Overview: Asks when a BigQuery table should be partitioned, when it should be clustered, and how each choice changes the bytes a query processes, its cost and its speed. Tests understanding of partition pruning, block pruning, column choice and the pitfalls of warehouse table design.