Skip to main content

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.

Beta

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 MODIFY privilege, 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

OPTIMIZE table_name

Compact files or incrementally cluster data using the existing keys.

OPTIMIZE table_name FULL

Recluster existing data as needed using the current clustering keys.

OPTIMIZE table_name EXTENDED

Run key selection and clustering on a table with automatic liquid clustering, or get a recommendation on other tables.

Command

Task

OPTIMIZE table_name

Compact files or incrementally cluster data using the existing keys.

OPTIMIZE table_name FULL

Recluster existing data as needed using the current clustering keys.

OPTIMIZE table_name EXTENDED

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.

  1. Verify that predictive optimization is enabled for the table. If necessary, a user with the required privileges can enable it:

    SQL
    ALTER TABLE main.benchmark.orders ENABLE PREDICTIVE OPTIMIZATION;
  2. Enable automatic liquid clustering on the test table:

    SQL
    ALTER 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:

    SQL
    ALTER 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.hierarchicalClusteringColumns table property before enabling automatic clustering. This example prioritizes region and keeps customer_id as a standard clustering key:

    SQL
    ALTER 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.

  3. Run queries that represent your benchmark workload, such as these customer filters:

    SQL
    SELECT sum(amount)
    FROM main.benchmark.orders
    WHERE customer_id BETWEEN 100 AND 200;

    SELECT sum(amount)
    FROM main.benchmark.orders
    WHERE customer_id BETWEEN 1000 AND 1100;
  4. Run key selection and clustering on demand:

    SQL
    OPTIMIZE 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_SELECTION

    11111111-1111-4111-8111-111111111111

    SUCCESS

    { "has_column_selection_changed": "true", "old_clustering_columns": "", "new_clustering_columns": "customer_id", "additional_reason": "New scheme is better than insertion order." }

    CLUSTERING

    22222222-2222-4222-8222-222222222222

    SUCCESS

    { "number_of_removed_files": "8", "number_of_clustered_files": "8", "amount_of_data_removed_bytes": "551799032", "amount_of_clustered_data_bytes": "517000000" }

    operation_type

    operation_id

    status

    detail

    AUTO_CLUSTERING_COLUMN_SELECTION

    11111111-1111-4111-8111-111111111111

    SUCCESS

    { "has_column_selection_changed": "true", "old_clustering_columns": "", "new_clustering_columns": "customer_id", "additional_reason": "New scheme is better than insertion order." }

    CLUSTERING

    22222222-2222-4222-8222-222222222222

    SUCCESS

    { "number_of_removed_files": "8", "number_of_clustered_files": "8", "amount_of_data_removed_bytes": "551799032", "amount_of_clustered_data_bytes": "517000000" }

  5. Verify the keys before rerunning your benchmark queries:

    SQL
    DESCRIBE DETAIL main.benchmark.orders;

    The selected output shows the chosen key:

    clusteringColumns

    ["customer_id"]

    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:

SQL
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:

JSON
{
"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:

SQL
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 detail fields

Standard clustering on a and b

{"recommended_clustering_columns":"`a`,`b`","recommended_hierarchical_clustering_columns":"","current_pruning_rate":"0.092045","top_evaluated_pruning_rate":"0.919580","calculated_pruning_improvement":"0.827535"}

Hierarchical clustering, prioritizing a before b

{"recommended_clustering_columns":"`a`,`b`","recommended_hierarchical_clustering_columns":"`a`,`b`","current_pruning_rate":"0.000000","top_evaluated_pruning_rate":"0.939542","calculated_pruning_improvement":"0.939542"}

Recommendation

Selected detail fields

Standard clustering on a and b

{"recommended_clustering_columns":"`a`,`b`","recommended_hierarchical_clustering_columns":"","current_pruning_rate":"0.092045","top_evaluated_pruning_rate":"0.919580","calculated_pruning_improvement":"0.827535"}

Hierarchical clustering, prioritizing a before b

{"recommended_clustering_columns":"`a`,`b`","recommended_hierarchical_clustering_columns":"`a`,`b`","current_pruning_rate":"0.000000","top_evaluated_pruning_rate":"0.939542","calculated_pruning_improvement":"0.939542"}

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 detail fields

Existing physical layout performs better

{"top_evaluated_clustering_columns":"`a`,`b`","recommended_clustering_columns":"","current_pruning_rate":"0.983363","top_evaluated_pruning_rate":"0.976190","calculated_pruning_improvement":"-0.007173"}

Evaluated layout matches the current layout

{"top_evaluated_clustering_columns":"`a`,`b`","recommended_clustering_columns":"","current_pruning_rate":"0.973684","top_evaluated_pruning_rate":"0.973684","calculated_pruning_improvement":"0.000000"}

Evaluation result

Selected detail fields

Existing physical layout performs better

{"top_evaluated_clustering_columns":"`a`,`b`","recommended_clustering_columns":"","current_pruning_rate":"0.983363","top_evaluated_pruning_rate":"0.976190","calculated_pruning_improvement":"-0.007173"}

Evaluated layout matches the current layout

{"top_evaluated_clustering_columns":"`a`,`b`","recommended_clustering_columns":"","current_pruning_rate":"0.973684","top_evaluated_pruning_rate":"0.973684","calculated_pruning_improvement":"0.000000"}

Review operation results​

The command reports the operations evaluated or run for your table:

Operation

Purpose

AUTO_CLUSTERING_COLUMN_SELECTION

Select clustering keys for a table with automatic liquid clustering.

AUTO_CLUSTERING_RECOMMENDATION

Recommend a layout without applying the recommended keys.

CLUSTERING

Cluster data using the selected keys.

COMPACTION

Compact small files.

ANALYZE

Collect table statistics.

DATA_SKIPPING_COLUMN_SELECTION

Select columns for data-skipping statistics.

Operation

Purpose

AUTO_CLUSTERING_COLUMN_SELECTION

Select clustering keys for a table with automatic liquid clustering.

AUTO_CLUSTERING_RECOMMENDATION

Recommend a layout without applying the recommended keys.

CLUSTERING

Cluster data using the selected keys.

COMPACTION

Compact small files.

ANALYZE

Collect table statistics.

DATA_SKIPPING_COLUMN_SELECTION

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

AUTO_CLUSTERING_COLUMN_SELECTION

NULL

SKIPPED

No eligible scan metrics found in the time window for column selection

operation_type

operation_id

status

detail

AUTO_CLUSTERING_COLUMN_SELECTION

NULL

SKIPPED

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.