Databricks DE ยท Topic 3.2

Liquid Clustering

The modern Delta data layout โ€” auto-organizes by clustering keys, replaces partitioning & ZORDER, and lets you change keys without rewriting data. This deep dive walks each with the mechanism and the code.

CLUSTER BYCLUSTER BY AUTOmax 4 keysOPTIMIZE FULLDBR 15.4 LTS+

The one-liner

Liquid Clustering replaces partitioning + ZORDER with automatic, key-based layout you can re-key without rewriting. Use CLUSTER BY (cols) or CLUSTER BY AUTO. Databricks recommends it for virtually all new tables (GA on DBR 15.4 LTS+).
๐Ÿ—„๏ธ Analogy: partitioning is a filing cabinet with fixed labeled drawers โ€” change the scheme, re-file everything. Liquid Clustering is a smart shelf that re-sorts as you add items, and you can change the sort rule anytime without re-shelving what's already there.

Layout approaches โ€” click to compare

โ–ธ partitioning vs ZORDER vs liquid clustering

Partitioning โ€” rigid directory structure per partition value.
  • โŒ Wrong key โ†’ small-file skew; changing it means a full rewrite
  • โŒ Bad for high-cardinality columns
ZORDER โ€” sorts/colocates within files on OPTIMIZE.
  • โš ๏ธ Must be re-applied on every OPTIMIZE
  • โš ๏ธ Needs manual tuning
Liquid Clustering โ€” automatic key-based layout.
  • โœ… Change keys without rewriting existing data
  • โœ… Handles skew & high cardinality; great for fast-growing tables
  • โœ… CLUSTER BY AUTO picks keys from query history
PartitioningZORDERLiquid Clustering
Change layout keyFull rewriteRe-run OPTIMIZENo rewrite
High cardinalityPoorOKGood
Combine with othersโ€”+ partitioning onlycannot mix
Recommended for new tablesNoNoYes

1 ยท Create a table & choose keys

Pick the 1โ€“4 columns you filter/join on most. Keys must be simple types (nested struct fields OK via dot notation; Array/Map/Struct columns are not). CLUSTER BY AUTO lets Predictive Optimization choose & adapt keys (DBR 15.4 LTS+, UC managed).

SQL โ€” explicit keys & AUTO

CREATE TABLE main.sales.events (id BIGINT, event_date DATE, region STRING)
  CLUSTER BY (event_date, region);          -- up to 4 keys

CREATE OR REPLACE TABLE main.sales.events2 (id BIGINT, region STRING)
  CLUSTER BY AUTO;                            -- Databricks picks & adapts keys

PySpark (DataFrameWriterV2)

(df.writeTo("main.sales.events").using("delta")
   .clusterBy("event_date", "region").create())

2 ยท Change keys without rewriting โ€” click each step

CREATE TABLE events (...) CLUSTER BY (event_date, region);
ALTER TABLE events CLUSTER BY (region);   -- re-key, no rewrite
OPTIMIZE events FULL;                      -- optional: recluster old data now (DBR 16.4 LTS+)
1 ยท CLUSTER BY (event_date, region)โ–ธ
Table is clustered by two keys. Writes & OPTIMIZE lay data out by these columns. (Up to 4 keys; simple types only.)
2 ยท ALTER โ€ฆ CLUSTER BY (region)โ–ธ
Query patterns shifted to region. Re-key instantly โ€” existing data is NOT rewritten; new writes use the new key. Impossible with partitioning.
3 ยท OPTIMIZE events FULLโ–ธ
Optional (DBR 16.4 LTS+). Forces immediate reclustering of legacy data to the new key when you don't want to wait for incremental writes.

3 ยท Clustering on write & maintenance

Clustering on write is automatic but only triggers once a write reaches a size threshold (โ‰ˆ64 MBโ€“1 GB depending on key count). Small/streaming writes may land unclustered until OPTIMIZE runs.

๐Ÿ’ก Because not every operation clusters, run OPTIMIZE regularly โ€” or rely on Predictive Optimization to do it automatically on UC managed tables. Structured Streaming to clustered tables needs DBR 16.4 LTS+.

Why it matters

๐Ÿ“… Real-world use case: a fast-growing events table is filtered by event_date now, by region next quarter. Just ALTER TABLE โ€ฆ CLUSTER BY (region) โ€” no full rewrite; future writes re-cluster.

Uses, edge cases & limitations

Common gotchas