What you will be able to do
- Explain how liquid clustering lets filters skip files, and why it replaces partitioning and Z-order
- Read a table's access patterns and decide whether, and on which columns, to cluster it
- Write CLUSTER BY at table creation and on an existing table, and use OPTIMIZE to apply the clustering
- Convert a partitioned Delta table to liquid clustering with REPLACE PARTITIONED BY WITH CLUSTER BY
Key concept
Liquid clustering — A data layout in which Databricks groups rows into files by the clustering keys you choose, so a filter on those keys can skip files that cannot match. Unlike partitions, the keys can be changed later as query patterns change.
1.How liquid clustering makes filters skip files
A filter is fast when the engine can avoid opening most of the table. Every write to a Delta table records per-file statistics: the minimum and maximum value of each column, null counts and row counts. At query time, Databricks compares the filter with those ranges and skips any file whose range cannot hold a match. The catch is that skipping only works when matching rows sit together. If customer_id = 12345 could appear in any file, every file's min–max range covers it and nothing gets skipped.
Liquid clustering fixes that by arranging data around the columns you name as clustering keys. Rows with similar key values end up in the same files, so each file's range becomes narrow and a filter on a key can rule out most files. It is a layout technique, and Databricks positions it as the replacement for both table partitioning and ZORDER. Clustering cannot be combined with either one on the same table.
Partitioning fails worst on the columns analysts filter on most, the ones with millions of distinct values. Hive-style partitioning writes one directory per value, and Databricks wants each partition to hold at least 1 GB. Below that size, partitioning tends to slow queries down. On size, the guidance is simple. Don't partition a table under 1 TB. Between 1 TB and 100 TB, use liquid clustering instead. At 100 TB and above, partitioning might help, but try liquid clustering first and confirm the gain. Liquid clustering handles both low- and high-cardinality columns and avoids the fixed partition boundaries and small files of static partitioning.
Z-order has a different weakness: nothing keeps it up to date. Z-ordering is applied when OPTIMIZE ... ZORDER BY rewrites files, so rows appended afterwards aren't ordered until someone runs it again. Predictive optimization, the automatic maintenance service, won't do that for you. When it runs OPTIMIZE, it never runs ZORDER and it ignores Z-ordered files. Adding compute doesn't help either. A larger SQL warehouse (warehouse sizing) reads the same unneeded files faster but reads no fewer of them, because the waste comes from the layout.
| Property | Partitioning (PARTITIONED BY) | Z-order (OPTIMIZE ... ZORDER BY) | Liquid clustering (CLUSTER BY) |
|---|---|---|---|
| High-cardinality filter column | Poor: many partitions below the 1 GB minimum | Works, but effectiveness drops with each extra column | Works for low and high cardinality |
| Changing the layout columns | Rigid; fixing a bad choice may need a full rewrite | Re-run OPTIMIZE with new columns | Redefine keys without rewriting existing data |
| Maintained by predictive optimization | No clustering; compaction happens within partitions | No: Z-ordered files are ignored | Yes: OPTIMIZE triggers incremental clustering |
Checkpoint 1 of 5· Match them up
An analyst's queries filter a 5 TB table on customer_id, which has millions of distinct values. Match each option to how it behaves for this workload.
Tap a term, then the definition that fits it.
Data skipping works only when matching rows sit together. Liquid clustering keeps a high-cardinality key co-located and is maintained automatically. Partitioning creates too many tiny partitions, Z-order goes stale as new data arrives, and more compute does nothing to reduce the files read.
“Liquid clustering works for both low and high cardinality columns and avoids the fixed partition boundaries and small-file issues common with static partitioning.”Source: docs.databricks.com
2.Reading a workload for clustering candidates
Choosing keys starts with how the table is actually queried. The clue usually shows up in Query Profile: most of a query's time goes to scanning files that don't match its filter. The columns in those WHERE clauses are your candidate keys. Databricks recommends liquid clustering for all new tables. It pays off most in these situations:
- Queries that filter on high-cardinality columns, such as customer or device IDs
- Tables with heavy data skew, where a few values hold most rows
- Fast-growing tables that would otherwise need constant maintenance and tuning
- Tables with concurrent writers, because liquid clustering allows row-level concurrency
- Tables whose access patterns vary or keep changing
- Tables where any likely partition key would return results from too many or too few partitions
Changing access patterns favour liquid clustering most clearly. When a team stops filtering by region and starts filtering by product line, you redefine the keys and existing data stays where it is. A partitioned table would need a full rewrite to make the same switch, and on a large table that is slow and expensive.
Some tables need no keys at all. Unpartitioned Delta tables are automatically clustered by ingestion time, which gives date filters partitioning-like performance with no tuning. Heavy UPDATE or MERGE traffic can break that natural order. In that case, Databricks recommends clustering on a column that follows ingestion order, such as an event timestamp or creation date.
Checkpoint 2 of 5· Check yourself
A 20 TB sales table is partitioned by region, but the analysts have stopped filtering by region and now filter mostly by product_line. Why is liquid clustering the better fit for this shift?
When access patterns change, liquid clustering lets you redefine the keys and leave existing data where it is. A partitioned table would need a full rewrite to make the same change, and that is slow and expensive on a large table. Clustering can't be combined with partitioning, and for tables between 1 TB and 100 TB the recommendation is liquid clustering, not partitioning.
“Unlike traditional partitioning, you can redefine clustering keys without rewriting existing data.”Source: docs.databricks.com
3.Declaring keys with CLUSTER BY and applying them with OPTIMIZE
To enable liquid clustering on a new table, add CLUSTER BY with the key columns to the CREATE TABLE statement. It is the only clause in the statement that turns on clustering. PARTITIONED BY creates partitions instead. ZORDER BY isn't a CREATE TABLE clause at all: it belongs to OPTIMIZE and can't be used on liquid-clustered tables. A made-up option such as OPTIONS (cluster_keys = ...) doesn't enable clustering.
CREATE TABLE table1 (
col0 STRING,
col1 DATE,
col2 BIGINT
)
CLUSTER BY (col0, col1);In a CREATE TABLE ... AS SELECT, put CLUSTER BY right after the table name, not inside the SELECT. To cluster an existing unpartitioned table, run ALTER TABLE <table_name> CLUSTER BY (<clustering_columns>). Clustered Delta tables use writer version 7 and reader version 3, and a Delta client that doesn't support those protocols can't read them.
Declaring keys and clustering data are separate steps, and OPTIMIZE does the second one. On a clustered table, OPTIMIZE rewrites files to group rows by the keys. It works incrementally, rewriting only the data that still needs clustering, so routine runs finish quickly. Some writes, such as INSERT INTO and CTAS, cluster data as they write, but only when a transaction passes a size threshold, so Databricks recommends running OPTIMIZE often. When you first enable clustering or change the keys, run OPTIMIZE <table_name> FULL to recluster all existing data. On a large table that has never been clustered on those keys, it can take hours.
If a table has queries that almost always filter or group on one column, you can make that column a hierarchical clustering key. Prioritizing a high-cardinality column this way can weaken clustering on the other keys, so weigh that tradeoff first.
Checkpoint 3 of 5· Fill the gap
Queries on this new table filter on col0 and col1, and the team wants keys they can redefine later without rewriting data. Which keyword completes the statement?
CREATE TABLE table1 (
col0 STRING,
col1 DATE,
col2 BIGINT
)
? BY (col0, col1);CLUSTER BY is the only CREATE TABLE clause that turns on liquid clustering, and liquid clustering keys can be redefined later. PARTITIONED BY creates fixed partitions instead. ZORDER BY belongs to OPTIMIZE, not CREATE TABLE, and can't be used on liquid-clustered tables.
Source: docs.databricks.com4.Converting a partitioned table to liquid clustering
Existing partitioned Delta tables can be converted in place. On Databricks Runtime 18.1 and above, ALTER TABLE ... REPLACE PARTITIONED BY WITH CLUSTER BY converts managed and external tables while keeping reader and writer downtime to a minimum. The conversion fixes the problems that usually prompt it: weak data skipping, over-partitioning and write conflicts. The CLUSTER BY clause at the end decides which keys the converted table starts with.
| Clause | Resulting clustering keys |
|---|---|
| CLUSTER BY (<clustering_columns>) | The columns you list; keep them close to the old partition columns |
| CLUSTER BY AUTO | Starts from the partition columns, then predictive optimization adapts them (Unity Catalog managed tables only) |
| CLUSTER BY (no options) | The current partition columns |
Picking new columns has a cost. If a table partitioned on (year, month, day) is converted to cluster on (day, id), the first OPTIMIZE has to recluster a lot of data, and the new keys do nothing until that OPTIMIZE has run. The old partition columns become hierarchical clustering keys automatically.
ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (day, id);
OPTIMIZE t1;To confirm the conversion, run DESCRIBE EXTENDED and check the new clustering columns. DESCRIBE HISTORY should show REORG operations, an UPGRADE PROTOCOL operation and the REPLACE PARTITIONED BY WITH CLUSTER BY operation (table history). To undo it, either recreate the table with CTAS, which keeps data written after the conversion, or restore an earlier table version.
Checkpoint 4 of 5· Check yourself
After running ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (day, id) on a table that was partitioned by (year, month, day), the team sees that queries filtering on id skip no more files than before. What explains this, and what is the fix?
When a conversion uses columns different from the old partitions, the data is not yet grouped by the new keys. The first OPTIMIZE does that reclustering, so the new keys only help once it has run. The old partition columns, not id, are the ones that become hierarchical keys automatically.
“To benefit from altering clustering columns, you must run OPTIMIZE.”Source: docs.databricks.com
Checkpoint 5 of 5· Check yourself
A team owns a partitioned external Delta table on Databricks Runtime 18.1. Its query patterns change often, and the team wants predictive optimization to adapt the clustering keys over time after conversion. What should they expect?
The conversion works on both managed and external tables, but the AUTO form depends on predictive optimization, which works only on Unity Catalog managed tables. AUTO starts from the partition columns. A plain CLUSTER BY keeps the partition columns as fixed keys and does not adapt them.
“AUTO: Uses the current partition columns as the initial clustering columns and lets predictive optimization adapt over time. Only available for Unity Catalog managed tables.”Source: docs.databricks.com
Sources2
Sources
(6)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“per-file statistics (minimum and maximum values, null counts, and total records) at query time to skip irrelevant files”
↩︎ How liquid clustering makes filters skip files - 2.https://docs.databricks.com/aws/en/tables/clusteringOfficial docs
“Liquid clustering is a data layout optimization technique that replaces table partitioning and ZORDER.”
↩︎ How liquid clustering makes filters skip files“Clustering is not compatible with partitioning or ZORDER.”
↩︎ How liquid clustering makes filters skip files“Queries that filter on high cardinality columns.”
↩︎ Reading a workload for clustering candidates“Tables where a typical partition key might return results from too many or too few partitions.”
↩︎ Reading a workload for clustering candidates“CLUSTER BY must appear after the table name, not in the SELECT clause”
↩︎ Declaring keys with CLUSTER BY and applying them with OPTIMIZE“Liquid clustering is incremental, meaning that OPTIMIZE only rewrites data as necessary to accommodate data that needs clustering.”
↩︎ Declaring keys with CLUSTER BY and applying them with OPTIMIZE“Because not all operations apply liquid clustering, Databricks recommends frequently running OPTIMIZE to ensure that all data is efficiently clustered.”
↩︎ Declaring keys with CLUSTER BY and applying them with OPTIMIZE“Delta Lake tables with liquid clustering enabled use Delta writer version 7 and reader version 3.”
↩︎ Declaring keys with CLUSTER BY and applying them with OPTIMIZE“Using very different columns triggers a large reclustering operation on the first OPTIMIZE run.”
↩︎ Converting a partitioned table to liquid clustering“To benefit from altering clustering columns, you must run OPTIMIZE.”
↩︎ Converting a partitioned table to liquid clustering“Conversion minimizes reader and writer downtime and supports both external and managed tables.”
↩︎ Converting a partitioned table to liquid clustering“Unlike traditional partitioning, you can redefine clustering keys without rewriting existing data.”
↩︎ Key concept“To force reclustering, use OPTIMIZE <table_name> FULL or OPTIMIZE <table_name> FULL WHERE <predicate>.”
↩︎ Exam trap 3“Unlike traditional partitioning, you can redefine clustering keys without rewriting existing data.”
↩︎ Checkpoint“The default behavior does not apply clustering to previously written data.”
↩︎ Prediction“AUTO: Uses the current partition columns as the initial clustering columns and lets predictive optimization adapt over time. Only available for Unity Catalog managed tables.”
↩︎ Checkpoint - 3.https://docs.databricks.com/aws/en/tables/partitionsOfficial docs
“For partitions, verify that each partition contains at least 1 GB of data.”
↩︎ How liquid clustering makes filters skip files“With more than 1 TB to 100 TB of data, use liquid clustering instead of partitioning.”
↩︎ How liquid clustering makes filters skip files“By using Delta Lake, unpartitioned tables automatically use ingestion time clustering.”
↩︎ Reading a workload for clustering candidates“An ineffective partitioning strategy might negatively affect query performance and require a full rewrite of data to fix.”
↩︎ Reading a workload for clustering candidates“Liquid clustering works for both low and high cardinality columns and avoids the fixed partition boundaries and small-file issues common with static partitioning.”
↩︎ Exam trap 1“Liquid clustering works for both low and high cardinality columns and avoids the fixed partition boundaries and small-file issues common with static partitioning.”
↩︎ Checkpoint - 4.
“Delta Lake tables without liquid clustering can optionally include a ZORDER BY clause to improve data clustering on rewrite.”
↩︎ How liquid clustering makes filters skip files - 5.
“On tables that use Z-order, predictive optimization ignores Z-ordered files.”
↩︎ How liquid clustering makes filters skip files“OPTIMIZE does not run ZORDER when executed by predictive optimization.”
↩︎ Exam trap 2 - 6.
“You can't use this clause on tables that use liquid clustering.”
↩︎ Declaring keys with CLUSTER BY and applying them with OPTIMIZE