Liquid Clustering in Databricks: How It Replaces Partitioning and ZORDER
What liquid clustering is, when to use it, how to turn it on for new, existing and partitioned Delta tables, how to choose clustering keys or let CLUSTER BY AUTO choose them, and what to check before switching.
Tecyfy · Published · Updated · 7 min read

Liquid clustering is a way of laying out a Delta table's data files so that queries filtering on a few chosen columns read fewer files. It replaces Hive-style partitioning and ZORDER: you name up to four clustering keys with CLUSTER BY, and OPTIMIZE sorts new data by them a little at a time. Unlike a partition column, the keys can be changed later without rewriting the table. Databricks now recommends it for all new tables.
This guide covers what problem it solves, how to turn it on for new and existing tables, how to choose keys, and the limits worth knowing before you switch.
The problem it replaces
A query that filters on a column only runs fast if the rows it needs sit together in a few files, so the engine can skip the rest using the minimum and maximum it stores for each file. Before liquid clustering, Delta tables had two ways to arrange that, and both had sharp edges:
- Partitioning writes each value of a column to its own folder. It works for a low-cardinality column such as a date, but a column with many values makes many tiny files, and a column with few values makes huge, uneven partitions. The choice is fixed when the table is created; changing it means rewriting everything.
ZORDER BYsorts the data inside each partition by several columns so that file statistics skip well. But eachOPTIMIZE ... ZORDER BYrewrites the files it touches again, which is expensive on a large table, and you have to remember to run it with the same columns every time.
Liquid clustering keeps the benefit, data grouped by the columns you filter on, without fixing the layout in folders. Clustering is incremental: OPTIMIZE only rewrites what needs it.
When to use it
Databricks recommends liquid clustering for all new tables, including streaming tables and materialized views. It helps most with:
- queries that filter on a high-cardinality column, such as a customer or device id;
- skewed data, where a few values hold most of the rows;
- fast-growing tables that would otherwise need regular tuning;
- tables written by several jobs at once;
- tables whose query patterns change, or where any partition key would give too many or too few partitions.
It is not compatible with partitioning or ZORDER on the same table: a table uses one or the other.
Turn it on for a new table
Add CLUSTER BY when you create the table:
CREATE TABLE sales.orders (
order_id BIGINT,
customer_id BIGINT,
order_date DATE,
amount DECIMAL(10, 2)
)
CLUSTER BY (customer_id, order_date);
From a DataFrame in Python:
(df.write
.clusterBy("customer_id", "order_date")
.saveAsTable("sales.orders"))
And from a streaming write, on Databricks Runtime 16.4 LTS and above:
(spark.readStream.table("sales.raw_orders")
.writeStream
.clusterBy("customer_id")
.option("checkpointLocation", "/Volumes/sales/checkpoints/orders")
.toTable("sales.orders"))
With the DataFrame API, clustering keys can only be set when a table is created or overwritten, not in append mode. To change the keys of a table you append to, use ALTER TABLE separately, as below.
Let Databricks choose the keys
On Unity Catalog managed tables (Databricks Runtime 15.4 LTS and above for Delta tables), CLUSTER BY AUTO hands the choice of keys to Databricks:
CREATE TABLE sales.orders (...) CLUSTER BY AUTO;
-- or, on an existing table
ALTER TABLE sales.orders CLUSTER BY AUTO;
Automatic clustering needs predictive optimization turned on. It looks at the table's query history, picks keys when it expects the saving to be worth the cost of clustering, and runs the clustering in the background. Databricks recommends it for choosing keys from your query patterns, though it may pick none for a table that is small or rarely queried.
Turn it on for an existing table
For an existing table that is not partitioned:
ALTER TABLE sales.orders CLUSTER BY (customer_id, order_date);
This changes the layout for data written from now on. To recluster the rows already in the table:
OPTIMIZE sales.orders FULL;
On a large table not yet clustered on these keys, this can take hours, so plan for the time and compute it takes.
A partitioned table needs one more step. On Databricks Runtime 18.1 and above, one statement converts it:
ALTER TABLE sales.orders REPLACE PARTITIONED BY WITH CLUSTER BY (customer_id, order_date);
On older runtimes, create a new clustered table from the old one with CREATE TABLE ... CLUSTER BY ... AS SELECT.
Choose the clustering keys
- Start from the filters. Use the columns your most frequent queries filter on. A key that no query filters by costs effort and saves nothing.
- Use few keys. You can name up to four. On tables under about 10 TB, more keys can make queries that filter on a single column slower: filtering with four keys does worse than with two. On larger tables the difference becomes negligible.
- Skip correlated columns. If two columns move together, such as an order date and an order timestamp, keep one.
- Check statistics. A key must be a column with statistics collected, which by default means one of the first 32 columns of the table.
Keys can be changed at any time with ALTER TABLE ... CLUSTER BY (...), and removed with CLUSTER BY NONE. Existing files keep their old layout until they are clustered again, which is what makes changing keys cheap.
Keep it clustered
New data is not clustered as it lands in every case, so run OPTIMIZE regularly, for example as the last task of the job that loads the table:
OPTIMIZE sales.orders;
It only works on files that need clustering, so frequent runs stay cheap. Databricks recommends letting the platform manage layout where it can: with predictive optimization on a Unity Catalog managed table, OPTIMIZE runs on its own. Run OPTIMIZE ... FULL when you first enable clustering on a table with data or change keys; once the layout matches the keys, it runs the same as OPTIMIZE.
What to check before switching
- Readers. A table with liquid clustering uses Delta writer version 7 and reader version 3. Any tool that reads the table must support those protocol versions, and the table cannot be downgraded afterwards. Check older Spark clusters and third-party readers first.
- Runtime. Liquid clustering is generally available for Delta tables from Databricks Runtime 15.4 LTS. Use a recent runtime; Databricks recommends the latest for the best performance.
- Outside Databricks. Liquid clustering is also part of open-source Delta Lake, with the same
CLUSTER BYsyntax.CLUSTER BY AUTOand predictive optimization are not covered in the open-source Delta Lake documentation; in Databricks they apply to Unity Catalog managed tables.
How much faster is it?
It depends entirely on how well the keys match your queries. In its announcement of general availability, Databricks reported 2 to 12 times faster reads for customers compared with partitioning and Z-ordering, and 7 times faster writes than partitioning plus ZORDER in its own benchmark. Treat those as what is possible, not what to expect: measure your own queries before and after, by comparing the files and bytes read in the query profile.
Tecyfy Takeaway
Liquid clustering groups a table's data by up to four columns you choose, or that Databricks chooses with CLUSTER BY AUTO, and keeps it that way with cheap, incremental OPTIMIZE runs. It replaces both partitioning and ZORDER, and unlike a partition column its keys can change as your queries do. Use it for new tables, pick keys from the filters you actually run, and check that every reader supports the newer Delta protocol before converting an old table.
Questions
- What is liquid clustering in Delta Lake?
- A way of laying out a Delta table's files so queries filtering on up to 4 chosen columns read fewer files. It replaces partitioning and ZORDER, and OPTIMIZE clusters new data incrementally. Unlike a partition column, the keys can be changed without rewriting the table.
- How do I enable liquid clustering on an existing table?
- For a table that is not partitioned, run ALTER TABLE name CLUSTER BY (columns), then OPTIMIZE name FULL to recluster the rows already there. A partitioned table converts with REPLACE PARTITIONED BY WITH CLUSTER BY on Databricks Runtime 18.1 and above.
- How many clustering keys should I use?
- As few as your queries need, up to 4. On tables under about 10 TB, more keys can make single-column filters slower, so 2 keys often beat 4. Choose columns your frequent queries filter on and skip columns that move together.
- What is CLUSTER BY AUTO?
- On Unity Catalog managed tables with predictive optimization on, CLUSTER BY AUTO lets Databricks pick the keys from the table's query history and run the clustering in the background.
- What should I check before switching to liquid clustering?
- That every tool reading the table supports Delta writer version 7 and reader version 3, because the table cannot be downgraded. Liquid clustering is generally available from Databricks Runtime 15.4 LTS and is also in open-source Delta Lake.
Sources