Sub topic 10
Partitioning, Z-order and liquid clustering
This chapter assumes you know basic SQL, Delta tables, and how data skipping uses file-level min/max statistics to avoid reading files. The goal is to choose how to lay out data so that filters read fewer files.
The layout problem
Data skipping only helps when similar values sit in the same files. If rows for one customer_id are scattered across every file, each file's min/max range covers almost everything, and no file can be skipped. Clustering strategies fix this by controlling which rows land together.
Partitioning
Partitioning splits a table into separate directories, one per distinct value of the partition column. A query that filters on that column reads only the matching directories.
Partitioning works well for a low-cardinality column, such as a date, on a very large table. It goes wrong in common ways:
- A high-cardinality column creates many tiny partitions and the small files problem.
- A partition column that queries rarely filter on adds cost and no benefit.
- Partition columns are fixed at creation. Changing them means rewriting the table.
As a rule of thumb, partition only when each partition holds at least about 1 GB of data. For most tables, prefer one of the two options below.
Z-ordering
Z-ordering is a technique applied during OPTIMIZE. It sorts rows along several columns at once so that similar values end up in the same files.
OPTIMIZE orders;
OPTIMIZE orders ZORDER BY (customer_id);The first statement only compacts small files. The second also co-locates rows by customer_id, so a filter on that column can prune more files.
Z-ordering has limits:
- You must rerun it as new data arrives, because it only reorganizes the files it processes.
- Each run can rewrite large parts of the table.
- Effectiveness drops as you add columns, so keep the list short.
Liquid clustering
Liquid clustering replaces both partitioning and Z-ordering for new tables. You declare the clustering keys with CLUSTER BY, and OPTIMIZE applies them.
CREATE TABLE events (
event_id BIGINT,
event_date DATE,
user_id BIGINT,
payload STRING
)
CLUSTER BY (event_date, user_id);
OPTIMIZE events;Liquid clustering is incremental, so OPTIMIZE rewrites only data that is not yet clustered. You can also change the keys later with ALTER TABLE ... CLUSTER BY, without rewriting the whole table. A table that uses liquid clustering cannot also be partitioned or Z-ordered.
Choosing a strategy
| Situation | Suggested choice |
|---|---|
| New table, filters on one or more columns | Liquid clustering |
| Very large table, always filtered by a low-cardinality column | Partitioning, possibly with clustering |
| Existing Z-ordered table that works well | Keep it until you have a reason to migrate |
| Query patterns change over time | Liquid clustering, since keys can be altered |
Exercise
A table has a user_id column with millions of distinct values, and most queries filter on it. A teammate proposes partitioning by user_id. What problem would this cause, and what would you use instead?
Show answer
Partitioning by a high-cardinality column creates millions of tiny partitions and many small files, which slows both reads and metadata operations. Use liquid clustering with CLUSTER BY (user_id) (or Z-order on an existing table) so rows are co-located without creating a directory per value.