Use automatic liquid clustering
With automatic liquid clustering, Databricks chooses clustering keys that optimize query performance. To enable it, use the CLUSTER BY AUTO clause.
For an overview of liquid clustering, including manually specified clustering keys, see Use liquid clustering for tables.
Automatic liquid clustering is also supported for materialized views and streaming tables, including Lakeflow pipelines and standalone pipelines. Specify CLUSTER BY AUTO in your pipeline or SQL definition.
Requirements
Automatic liquid clustering has the following requirements:
- Unity Catalog managed Delta Lake tables require Databricks Runtime 15.4 LTS and above.
- Unity Catalog managed Apache Iceberg v3 tables require Databricks Runtime 18 LTS and above.
- Predictive optimization must be enabled. See Predictive optimization for Unity Catalog managed tables.
How automatic liquid clustering works
Automatic liquid clustering uses predictive optimization for automatic key selection and clustering operations, and runs asynchronously.
Automatic liquid clustering adjusts to your usage patterns:
- Analyzes query workload: Databricks analyzes the table's historical query workload and identifies the best candidate columns for clustering.
- Adapts to changes: If your query patterns or data distributions change over time, automatic liquid clustering selects new keys to optimize performance.
- Cost-aware selection: Databricks changes clustering keys only when the predicted cost savings from data skipping improvements outweigh the data clustering cost.
Automatic liquid clustering might not select keys for the following reasons:
- The table is too small to benefit from liquid clustering.
- The table already has an effective clustering scheme, either from previous manual keys or natural insertion order that matches query patterns.
- The table doesn't have frequent queries.
- You aren't using Databricks Runtime 15.4 LTS or above.
To see why predictive optimization skipped clustering key selection for a specific table, check the History tab in Catalog Explorer. To view the skip reason, click the AUTO LIQUID row with the Not applied label in the Operation column. See Use the history tab in Catalog Explorer.
You can apply automatic liquid clustering for all Unity Catalog managed tables, regardless of data and query characteristics. Predictive optimization decides whether selecting clustering keys is cost-beneficial.
Select keys and optimize on demand
This feature is in Beta. To use it, a workspace admin must turn on OPTIMIZE EXTENDED from the Previews page. See Manage Databricks previews.
Use OPTIMIZE table_name EXTENDED to select clustering keys and cluster a table on demand for a proof of concept or benchmark, or to reassess a changed workload. Run representative queries first. See Optimize table layout with OPTIMIZE EXTENDED for the walkthrough and example outputs.
Databricks Runtime version compatibility
You can read or write tables with automatic clustering enabled from all Databricks Runtime versions that support liquid clustering. Automatic key selection relies on metadata introduced in Databricks Runtime 15.4 LTS. For the Databricks Runtime versions that can read and write tables with liquid clustering, see Compatibility for tables with liquid clustering.
Use Databricks Runtime 15.4 LTS or above to ensure that automatically selected keys benefit all of your workloads and that these workloads are considered when selecting new keys.
Enable or turn off automatic liquid clustering
Use SQL or the Python API to enable automatic liquid clustering on a new or existing table, set initial clustering key hints, or turn it off.
- SQL
- Python
To create a table with automatic liquid clustering:
CREATE OR REPLACE TABLE table1 (column01 int, column02 string) CLUSTER BY AUTO;
To enable automatic liquid clustering on an existing table, including tables with manually specified keys:
ALTER TABLE table1 CLUSTER BY AUTO;
To set initial clustering column hints for key selection, set the clustering keys and then turn on automatic clustering:
ALTER TABLE table1 CLUSTER BY (column01, column02);
ALTER TABLE table1 CLUSTER BY AUTO;
Alternatively, use the Python API to set hints in a single operation.
To turn off automatic liquid clustering:
ALTER TABLE table1 CLUSTER BY NONE;
To turn off automatic liquid clustering and specify clustering columns:
ALTER TABLE table1 CLUSTER BY (column01, column02);
If an existing table has automatic liquid clustering enabled, running CREATE OR REPLACE table_name without CLUSTER BY AUTO turns off automatic clustering and does not preserve clustering columns. To preserve automatic liquid clustering and any previously selected columns, include CLUSTER BY AUTO in the replace statement. With CLUSTER BY AUTO, predictive optimization uses the historical query workload for the table to identify the best clustering keys.
The Python API is available in Databricks Runtime 16.4 LTS and above. To change automatic clustering without rewriting the table's data, use SQL.
To create a table with automatic liquid clustering using DataFrameWriter:
df = spark.read.table("table1")
(df.write
.format("delta")
.option("clusterByAuto", "true")
.saveAsTable("target_table")
)
To set initial clustering column hints for key selection using DataFrameWriter:
(df.write
.format("delta")
.clusterBy("clusteringColumn1", "clusteringColumn2")
.option("clusterByAuto", "true")
.saveAsTable("target_table")
)
To create a table with automatic liquid clustering using DataFrameWriterV2:
(df.writeTo("target_table")
.using("delta")
.option("clusterByAuto", "true")
.create()
)
To set initial clustering column hints for key selection using DataFrameWriterV2:
(df.writeTo("target_table")
.using("delta")
.clusterBy("clusteringColumn1", "clusteringColumn2")
.option("clusterByAuto", "true")
.create()
)
To enable automatic liquid clustering on an existing Delta Lake table with DataFrameWriter, or to keep it when you overwrite a table that already uses it, overwrite the entire table using saveAsTable() with overwriteSchema and clusterByAuto set to true:
df = spark.read.table("source_table")
(df.write
.format("delta")
.mode("overwrite")
.option("overwriteSchema", "true")
.option("clusterByAuto", "true")
.saveAsTable("target_table"))
With saveAsTable(), a full-table overwrite with overwriteSchema set to true turns off automatic clustering and clears the keys if you omit both clusterByAuto and .clusterBy().
With DataFrameWriterV2, to enable automatic clustering on an existing Delta Lake table, set clusterByAuto to true and call replace() or createOrReplace().
To create a streaming table with automatic liquid clustering:
(spark.readStream.table("source_table")
.writeStream
.option("clusterByAuto", "true")
.option("checkpointLocation", checkpointPath)
.toTable("target_table")
)
To set initial clustering column hints for key selection in a streaming table:
(spark.readStream.table("source_table")
.writeStream
.clusterBy("column1", "column2")
.option("clusterByAuto", "true")
.option("checkpointLocation", checkpointPath)
.toTable("target_table")
)
When you use .clusterBy for cluster key selection hints together with .option('clusterByAuto', 'true'), the behavior is as follows:
- If this sets automatic liquid clustering for the first time, the clustering columns are set to the specified columns in
.clusterBy. - If this is an existing table with automatic liquid clustering enabled, a
.clusterByhint is accepted only one time. The columns specified by.clusterByare set only if the table doesn't already have clustering columns.
For Delta Lake tables, append mode doesn't change the automatic clustering setting. With saveAsTable(), setting clusterByAuto to true on a table that doesn't have automatic clustering fails with DELTA_CLUSTER_BY_AUTO_MISMATCH in append mode or when overwriting without schema replacement. For overwrites without schema replacement or clustering options, see Create tables with clustering.
To change automatic clustering separately from an append, use SQL ALTER TABLE.
Check if automatic clustering is enabled
To check if a table has automatic liquid clustering enabled, use DESCRIBE TABLE or SHOW TBLPROPERTIES.
If automatic liquid clustering is enabled, the clusterByAuto property is set to true. The clusteringColumns property shows the current clustering columns that were automatically or manually selected.
Limitations
Automatic liquid clustering isn't available for managed Apache Iceberg v2 tables. It is supported for managed Apache Iceberg v3 tables in Databricks Runtime 18 LTS and above.