Sub topic 7
Liquid clustering: flexible, incremental layout
This chapter assumes you know how OPTIMIZE compacts files, how Z-ordering co-locates rows, and how data skipping uses per-file min/max statistics. It also assumes you know what Hive-style partitioning does. Liquid clustering is Databricks' replacement for both partitioning and Z-ordering on new tables.
The problem it solves
Partitioning fixes your layout at creation time. Changing the partition columns means rewriting the whole table. Z-ordering is also awkward, because each OPTIMIZE ... ZORDER BY run re-sorts the files in scope, and you must remember to pass the right columns every time.
Liquid clustering moves the layout choice into table metadata. You declare clustering columns once with CLUSTER BY. Each OPTIMIZE run then clusters only data that is not yet clustered, so the work is incremental.
Creating a clustered table
You use CLUSTER BY where you would otherwise write PARTITIONED BY:
CREATE TABLE events_clustered (
event_id BIGINT,
user_id BIGINT,
event_date DATE,
payload STRING
)
CLUSTER BY (event_date, user_id);
OPTIMIZE events_clustered;Writing data does not by itself produce a fully clustered layout. Clustering is applied when you run OPTIMIZE, or when predictive optimization runs it for you on managed tables. Run OPTIMIZE without a WHERE clause or ZORDER BY. The clustering columns come from the table definition.
How an OPTIMIZE run behaves
The diagram below shows the cycle.
Files that are already clustered are generally left alone. This keeps repeated OPTIMIZE runs cheaper than repeated Z-ordering of the same data.
Changing clustering columns
You can change the keys on an existing table with ALTER TABLE, or remove clustering altogether:
ALTER TABLE events_clustered CLUSTER BY (user_id);
ALTER TABLE events_clustered CLUSTER BY NONE;The change does not rewrite existing data immediately. New writes and later OPTIMIZE runs use the new keys, while older files keep their previous layout until they are rewritten. If your query patterns shift, you adjust the keys instead of rebuilding the table.
Differences from partitioning and Z-ordering
| Aspect | Partitioning | Z-ordering | Liquid clustering |
|---|---|---|---|
| Declared in | `PARTITIONED BY` | `OPTIMIZE ... ZORDER BY` | `CLUSTER BY` |
| Change the keys later | Requires a table rewrite | Pass different columns on the next run | `ALTER TABLE ... CLUSTER BY` |
| Work per run | None, since layout follows directory | Rewrites files in scope | Targets unclustered data |
| Combined with partitioning | Not applicable | Yes | No |
Common errors
- Expecting clustering on write alone. If a table looks unclustered after a load, run
OPTIMIZEand check the result. - Combining
CLUSTER BYwithPARTITIONED BYorZORDER BY. A table uses one layout strategy. Liquid clustering cannot be used together with partitioning, andZORDER BYis not used on a clustered table. - Too many clustering columns. Each added column dilutes the benefit for the others. Choose only the columns that queries filter on most often. The next chapters cover this in more detail.
- Using a column without statistics. Skipping relies on file statistics. Check that your clustering columns are among those that have statistics collected.
Exercise
A team has a table created with CLUSTER BY (event_date). Their queries now mostly filter on user_id. What is the cheapest way to adapt the layout, and when do existing files take on the new layout?
Show answer
Run ALTER TABLE ... CLUSTER BY (user_id). This changes only metadata, so no table rewrite is needed. New writes and later OPTIMIZE runs use the new key, and existing files are reclustered as OPTIMIZE processes them.