Optimize table layout with OPTIMIZE EXTENDED
Use OPTIMIZE table_name EXTENDED to prepare your table for a proof of concept or benchmark without guessing which clustering keys to use. Run representative queries, then let the command choose a layout based on your workload.
On a table with automatic liquid clustering, the command runs key selection and clustering on demand. On other tables, it returns a clustering recommendation without applying the recommended keys. Use it again when your query patterns change or to check whether your existing clustering keys suit your workload.
This feature is in Beta. To use it, a workspace admin must turn on OPTIMIZE EXTENDED from the Previews page. See Manage Databricks previews.
Requirements
- Use a Unity Catalog managed table with predictive optimization enabled.
- Own the table or have the
MODIFYprivilege, including when you only want a recommendation. The command can also schedule maintenance. - Use any supported compute type. On Databricks Runtime compute, use version 19 or above.
Use a named table reference. Path-based references, materialized views, and streaming tables are not supported.
Choose an optimization command
Choose the command that matches your task:
Command | Task |
|---|---|
| Compact files or incrementally cluster data using the existing keys. |
| Recluster existing data as needed using the current clustering keys. |
| Run key selection and clustering on a table with automatic liquid clustering, or get a recommendation on other tables. |
Key selection can retain the existing keys. A recommendation does not replace manually selected keys or convert a partitioned table to liquid clustering.
Prepare a table for a benchmark
For clustering-key selection and recommendations, EXTENDED uses a 1-day query-history lookback by default. Run your representative queries first, then run the command.
To prepare a table with automatic liquid clustering for a benchmark, follow these steps. The examples use an existing Unity Catalog managed Delta Lake test table, main.benchmark.orders, with customer_id, region, and amount columns.
-
Verify that predictive optimization is enabled for the table. If necessary, a user with the required privileges can enable it:
SQLALTER TABLE main.benchmark.orders ENABLE PREDICTIVE OPTIMIZATION; -
Enable automatic liquid clustering on the test table:
SQLALTER TABLE main.benchmark.orders CLUSTER BY AUTO;To set initial clustering column hints for key selection, set the clustering keys before enabling automatic liquid clustering:
SQLALTER TABLE main.benchmark.orders CLUSTER BY (customer_id, region);ALTER TABLE main.benchmark.orders CLUSTER BY AUTO;For a hierarchical hint, also set the
delta.liquid.hierarchicalClusteringColumnstable property before enabling automatic clustering. This example prioritizesregionand keepscustomer_idas a standard clustering key:SQLALTER TABLE main.benchmark.orders CLUSTER BY (region, customer_id);ALTER TABLE main.benchmark.orders SET TBLPROPERTIES ('delta.liquid.hierarchicalClusteringColumns' = 'region');ALTER TABLE main.benchmark.orders CLUSTER BY AUTO;Every hierarchical column must also be a clustering key. See Prioritize clustering keys with hierarchical clustering.
-
Run queries that represent your benchmark workload, such as these customer filters:
SQLSELECT sum(amount)FROM main.benchmark.ordersWHERE customer_id BETWEEN 100 AND 200;SELECT sum(amount)FROM main.benchmark.ordersWHERE customer_id BETWEEN 1000 AND 1100; -
Run key selection and clustering on demand:
SQLOPTIMIZE main.benchmark.orders EXTENDED;The following illustrative output shows selected rows from a run without initial hints. Operation IDs and metrics vary by run.
operation_type
operation_id
status
detail
AUTO_CLUSTERING_COLUMN_SELECTION11111111-1111-4111-8111-111111111111SUCCESS{"has_column_selection_changed": "true","old_clustering_columns": "","new_clustering_columns": "customer_id","additional_reason": "New scheme is better than insertion order."}CLUSTERING22222222-2222-4222-8222-222222222222SUCCESS{"number_of_removed_files": "8","number_of_clustered_files": "8","amount_of_data_removed_bytes": "551799032","amount_of_clustered_data_bytes": "517000000"} -
Verify the keys before rerunning your benchmark queries:
SQLDESCRIBE DETAIL main.benchmark.orders;The selected output shows the chosen key:
clusteringColumns
["customer_id"]Compare query performance using comparable compute and cache conditions. Selected keys do not guarantee a particular query speedup.
Reassess a changed workload
After a workload change, run the new representative queries and repeat OPTIMIZE table_name EXTENDED. The evaluation uses the last day of query history by default. For example, run queries filtering on region before requesting another evaluation:
SELECT sum(amount) FROM main.benchmark.orders WHERE region = 17;
SELECT sum(amount) FROM main.benchmark.orders WHERE region = 231;
OPTIMIZE main.benchmark.orders EXTENDED;
Selection can keep the existing keys. The following example detail shows a successful evaluation that retains customer_id:
{
"has_column_selection_changed": "false",
"old_clustering_columns": "customer_id",
"additional_reason": "No column selection change is needed, the existing clustering scheme is better."
}
When EXTENDED finds a potentially better layout and schedules validation, it saves the query-history starting point for later automatic clustering decisions. This starting point remains saved even if validation fails or the clustering keys stay the same.
Get a clustering recommendation
To assess an existing table's layout without enabling automatic liquid clustering, run the command on that table:
OPTIMIZE main.benchmark.existing_orders EXTENDED;
The AUTO_CLUSTERING_RECOMMENDATION row can recommend standard or hierarchical clustering. These example detail excerpts use workload columns named a and b:
Recommendation | Selected |
|---|---|
Standard clustering on |
|
Hierarchical clustering, prioritizing |
|
In the standard clustering example, the estimated pruning improvement is about 82.75 percentage points. This is an estimate of data skipping, not a measured query speedup. The recommendation does not apply the keys. See Prioritize clustering keys with hierarchical clustering to configure a hierarchy manually.
Empty recommendation fields mean that the evaluation did not produce a recommended change. The result can still show the best evaluated candidate:
Evaluation result | Selected |
|---|---|
Existing physical layout performs better |
|
Evaluated layout matches the current layout |
|
Review operation results
The command reports the operations evaluated or run for your table:
Operation | Purpose |
|---|---|
| Select clustering keys for a table with automatic liquid clustering. |
| Recommend a layout without applying the recommended keys. |
| Cluster data using the selected keys. |
| Compact small files. |
| Collect table statistics. |
| Select columns for data-skipping statistics. |
Applicable operations vary by table and workload.
Check each operation's status and detail for skipped work or successful operations that rewrite no files. detail can contain JSON metrics or a plain-text skip reason. See EXTENDED output for the full result schema and metrics fields.
Use representative data and queries that scan the table. Queries served entirely from the result cache do not produce new table scans, and small tables can be ineligible for key selection even after you query them. For example, a skipped selection can return:
operation_type | operation_id | status | detail |
|---|---|---|---|
|
|
| No eligible scan metrics found in the time window for column selection |
To look up maintenance metrics and compute usage, copy a non-null operation_id into the operation-history query. Records typically appear within 12 hours.
Costs and limitations
The command uses billable predictive optimization compute and waits for triggered operations to finish. It can take several minutes. Applicable maintenance, including file compaction and statistics collection, can run even when the command returns a recommendation. See Compute and billing.
When scheduling a new clustering operation for a liquid-clustered table up to 10 TB, EXTENDED uses OPTIMIZE FULL to recluster existing data as needed. It uses incremental OPTIMIZE for larger tables or when the table size is unknown. An existing pending or running clustering operation is reused without changing its mode.
You cannot combine EXTENDED with FULL, WHERE, ZORDER BY, or DRY RUN.
Let predictive optimization manage routine maintenance automatically. Use EXTENDED when preparing a benchmark or reassessing a changed workload.