Sub topic 8
Choosing clustering and Z-order columns
This chapter assumes you can read basic SQL, know that Delta tables store min/max statistics per file, and have run OPTIMIZE before. The same selection rules apply to ZORDER BY and CLUSTER BY, so the chapter treats them together.
What the choice controls
Both techniques sort rows so that similar values land in the same files. A query that filters on a chosen column can then skip files whose min/max range cannot match. Columns you did not choose get no benefit. The job is to pick the few columns where skipping pays off most.
Rules for picking columns
- Pick columns that appear in filters. Look at your real queries. Columns used in
WHERE, join conditions that become filters, andMERGEmatch conditions are candidates. Columns that are only selected or aggregated are not. - Prefer columns that are filtered often and selectively. A column used in most queries that returns a small fraction of rows beats one used rarely.
- Prefer higher cardinality. Cardinality is the number of distinct values. A column like
user_idhas high cardinality, so each file covers a narrow range. A column likeis_activehas two values, so every file contains both and nothing is skipped. - Avoid extremely unique, unfilterable values such as random UUIDs used only for point lookups, unless you really filter on them. Ordering by them helps only if queries target them.
- Keep the list short. Each added column dilutes the effect of the others.
-- Good: filtered often, many distinct values
CLUSTER BY (event_date, user_id)
-- Weak: two distinct values, no skipping gain
CLUSTER BY (is_active)Why more columns help less
Z-ordering and liquid clustering both interleave several columns in one ordering. With one column, files are tightly grouped on it. With four, each column's ranges become wider, because the layout has to serve all four at once. The result is weaker skipping per column.
OPTIMIZE events_partitioned
WHERE event_date >= '2024-01-01'
ZORDER BY (user_id);A single well-chosen column is a good starting point. Add a second only when queries filter on it independently and often.
Differences between the two techniques
| Aspect | Z-order | Liquid clustering |
|---|---|---|
| Where columns are set | In each `OPTIMIZE ... ZORDER BY` command | Once, on the table with `CLUSTER BY` |
| Changing columns | Pass different columns next run | `ALTER TABLE ... CLUSTER BY` |
| Column order | Not meaningful | Not meaningful |
| Typical limit | Fewer columns works best | Up to 4 columns |
Z-order columns must be among those with statistics. Partition columns cannot be Z-order columns.
Changing your mind
If query patterns shift, liquid clustering lets you change the keys without rewriting everything up front:
ALTER TABLE events_clustered CLUSTER BY (user_id);New keys apply to data as later OPTIMIZE runs rewrite files. Existing files keep their old layout until then.
Deciding in practice
Common mistakes
- Choosing columns by guesswork instead of from query history.
- Clustering on a low-cardinality flag.
- Listing many columns hoping to cover everything.
- Picking a column that has no statistics.
Exercise
A table has columns region (5 distinct values), customer_id (millions), notes (free text, never filtered) and order_date. Most queries filter on order_date and customer_id. Which columns do you pick for CLUSTER BY, and why do you leave out the others?
Show answer
Use CLUSTER BY (order_date, customer_id). Both are filtered often and have many distinct values, so files get narrow min/max ranges and queries skip more of them. region has only 5 values, so almost every file would contain all regions and skipping would barely improve. notes is never filtered, so clustering on it gives no benefit.