Sub topic 13
Choosing between partitioning, Z-ordering, and liquid clustering
This chapter assumes you know how OPTIMIZE compacts files, how data skipping uses file statistics, and the basic syntax for PARTITIONED BY, ZORDER BY, and CLUSTER BY. Here you will pick one layout strategy for a table and avoid the usual mistakes.
What each strategy does
All three change which rows share a file, so that filters can skip files. They differ in how they do it and how much upkeep they need.
| Strategy | How it groups rows | Upkeep | Main limit |
|---|---|---|---|
| Partitioning | One directory per distinct value | Fixed at table creation | Too many partitions create small files |
| Z-ordering | Rewrites files in scope so similar values sit together | You run `OPTIMIZE ... ZORDER BY` | Each run rewrites all files in scope, and more columns dilute the benefit |
| Liquid clustering | Clusters files by `CLUSTER BY` columns | `OPTIMIZE` clusters new data incrementally | Cannot be combined with partitioning or `ZORDER BY` on the same table |
A decision path
Liquid clustering is the default choice for new tables because you can change the keys later. Databricks recommends it for new tables and says it replaces both partitioning and Z-ordering.
When partitioning still fits
Partition only when all of these hold:
- The column has low cardinality, such as a date or a region.
- Most queries filter on it.
- Each partition would hold roughly 1 GB or more, which usually means the table is over about 1 TB.
Partitioning also helps when you drop or reload whole partitions. A partition column cannot be changed without rewriting the table.
When Z-ordering fits
Use Z-ordering on a table that must stay on an older layout, or on partitioned tables where you still need skipping on a second column:
OPTIMIZE events_partitioned
WHERE event_date >= '2024-01-01'
ZORDER BY (user_id);Restrict the WHERE clause to recent partitions. Without it, every run rewrites old data that has not changed.
When liquid clustering fits
Choose liquid clustering when filter patterns change, the table grows quickly, or the right partition column is unclear.
CREATE TABLE events_clustered (
event_id BIGINT,
user_id BIGINT,
event_date DATE,
payload STRING
)
CLUSTER BY (event_date, user_id);
OPTIMIZE events_clustered;If query patterns shift later, change the keys with ALTER TABLE events_clustered CLUSTER BY (user_id);. Existing files are not rewritten at that moment. New clustering applies as OPTIMIZE runs.
Common errors
- Partitioning on a high-cardinality column. A partition per
user_idcreates millions of tiny files. - Partitioning a small table. Each partition holds too little data, so file overhead outweighs any skipping.
- Z-ordering on too many columns. The skipping benefit shrinks with each added column. Pick one to three.
- Mixing strategies.
CLUSTER BYandPARTITIONED BYcannot be declared together, and a clustered table cannot useZORDER BY. - Expecting clustering without
OPTIMIZE. Clustering is applied whenOPTIMIZEruns, or by predictive optimization on managed tables. - Never measuring. Compare files scanned and bytes read before and after, as described in the monitoring chapter.
Exercise
A 200 GB table is queried mostly by customer_id (millions of values) and sometimes by order_date. The filter columns may change next quarter. Which strategy would you choose, and why not partition by customer_id?
Show answer
Use liquid clustering with CLUSTER BY (customer_id, order_date). The table is small for partitioning, the filters may change, and the keys can be altered later. Partitioning by customer_id would create millions of partitions, each with tiny files, which is the small-files problem again.