Sub topic 16
Automating maintenance with predictive optimization
This chapter assumes you have read the earlier chapters on Delta file layout, OPTIMIZE, liquid clustering, and table statistics. You should know why small files and stale statistics slow queries down, and you should be comfortable running SQL against Unity Catalog tables.
What predictive optimization does
Earlier chapters had you run OPTIMIZE and ANALYZE TABLE by hand. That works for a few tables. It does not scale to hundreds of tables, because someone has to decide which tables need work, when to run it, and how much compute to spend.
Predictive optimization moves that decision to Databricks. For tables it manages, it watches usage and table state, then schedules maintenance operations on its own. The operations are:
| Operation | What it does | Why it helps queries |
|---|---|---|
| `OPTIMIZE` | Compacts small files and applies clustering | Fewer files to open, better data skipping |
| `VACUUM` | Deletes data files no longer referenced by the table | Lower storage cost |
| `ANALYZE` | Collects statistics | Better join and scan decisions by the optimizer |
Predictive optimization decides per table whether an operation is worth running now. A table that has not changed since its last compaction is left alone. A table that receives many small appends is compacted more often.
Where it applies
Check these conditions before you rely on it:
- It applies to Unity Catalog managed tables. It does not maintain external tables or tables outside Unity Catalog.
- The maintenance runs on serverless compute managed by Databricks, not on your clusters or warehouses. It is billed separately, so it does not slow down your own workloads.
- It follows a hierarchy: account, catalog, schema, table. A setting at a lower level can override or inherit from a higher one.
- Availability depends on your region and account. Newer accounts may have it enabled by default, and older ones may need an admin to turn it on. Check the current Databricks documentation for your cloud.
- It does not replace a layout choice. If your table uses Z-ordering, predictive optimization does not run
ZORDER BYfor you. Liquid clustering is the layout it works with best.
Enabling and inheriting the setting
An account admin must first allow predictive optimization at the account level. After that, you control it per catalog or schema with SQL.
-- Turn it on for every table in a catalog
ALTER CATALOG main ENABLE PREDICTIVE OPTIMIZATION;
-- Turn it off for one schema that you maintain by hand
ALTER SCHEMA main.staging DISABLE PREDICTIVE OPTIMIZATION;
-- Go back to following the parent catalog
ALTER SCHEMA main.staging INHERIT PREDICTIVE OPTIMIZATION;INHERIT is the default state. A schema in INHERIT mode takes whatever its catalog says. This lets you enable the feature once on a catalog and opt out only the few schemas that need special handling.
To see whether a table is covered, inspect it:
DESCRIBE EXTENDED main.sales.orders;Look in the output for the predictive optimization row. It shows whether the setting is enabled, disabled, or inherited for that table.
How a decision flows
The diagram shows the order in which a table is considered. Not every table gets every operation on every run.
Verifying what it did
Automation you cannot inspect is hard to trust. Databricks records each operation in a system table. Query it to see which tables were maintained, which operation ran, and what it cost.
SELECT
catalog_name,
schema_name,
table_name,
operation_type,
operation_status,
usage_quantity,
usage_unit,
start_time
FROM system.storage.predictive_optimization_operations_history
WHERE start_time >= current_date() - INTERVAL 7 DAYS
ORDER BY start_time DESC;You need access to the system.storage schema, which an admin must grant. The usage_quantity and usage_unit columns show the billed consumption of each run. Summing them per table tells you which tables cost the most to maintain.
A useful check after enabling the feature is to compare the file count and file sizes of a busy table before and after a run:
DESCRIBE DETAIL main.sales.orders;The numFiles and sizeInBytes columns let you compute average file size. If numFiles drops and the average size rises after a successful OPTIMIZE entry in the history table, the operation did its job.
Pairing it with automatic layout
Predictive optimization is most useful when the table layout is declarative. With liquid clustering you state the clustering columns once:
CREATE TABLE main.sales.events (
event_id BIGINT,
event_date DATE,
user_id BIGINT,
payload STRING
)
CLUSTER BY (event_date, user_id);Predictive optimization then keeps applying the clustering when it runs OPTIMIZE. You no longer schedule the OPTIMIZE events; job yourself. Databricks also offers CLUSTER BY AUTO, where it chooses the clustering keys based on query history. Whether it is available depends on your runtime and account, so confirm in the documentation before you plan around it.
Common errors and gotchas
- Nothing seems to happen. Check the three usual causes in order: the table is external, the setting is disabled or inherited as disabled, or the history table has not been granted to you. A table also needs enough change or usage to qualify, so a tiny static table may never be touched.
- You expected immediate results. Maintenance is scheduled, not instant. After a large load, query performance may stay poor until the next run. If you need a table optimized right now, run
OPTIMIZEmanually. The two approaches can coexist. - Double work. If an existing job still runs
OPTIMIZEandVACUUMnightly on the same table, you pay for both. Remove the manual jobs once you have confirmed that predictive optimization covers the table. - Vacuum retention surprises.
VACUUMremoves files older than the retention period. Time travel and long-running readers need those files, so do not shorten the retention period to save storage unless you understand the effect. - Z-ordered tables. Because it does not run
ZORDER BY, a Z-ordered table may get compaction but lose its sort order over time. Consider migrating such tables to liquid clustering. - Cost is not zero. Serverless maintenance consumes billed usage. On a large estate, review the history table monthly and disable the feature for schemas whose tables are rarely queried.
When to prefer manual control
Choose manual maintenance when you need a specific schedule, such as optimizing right after a nightly load and before the morning dashboards. Choose it also for external tables, which predictive optimization cannot touch, or for a one-off backfill where you want a custom ZORDER BY or a full ANALYZE TABLE ... COMPUTE STATISTICS FOR ALL COLUMNS. For everything else, enable it at the catalog level and review the history.
Exercise
A team enables predictive optimization on the catalog main. A week later, the table main.raw.clicks shows no entries in system.storage.predictive_optimization_operations_history, although it receives thousands of small files per day. Name three things you would check.
Show answer
First, check whether main.raw.clicks is a Unity Catalog managed table, because external tables are not maintained. Second, check whether its schema raw or the table itself is set to DISABLE, which overrides the catalog setting, for example with DESCRIBE EXTENDED main.raw.clicks. Third, confirm that you have been granted access to the system.storage schema, since an empty result can also mean missing permissions or an incorrect date filter. Also consider that the account-level setting may not be enabled.
Exercise
Why might you still run OPTIMIZE orders ZORDER BY (customer_id); by hand on a table even though predictive optimization is enabled?
Show answer
Predictive optimization does not run ZORDER BY, so a Z-ordered layout is only maintained by your own command. A manual run also gives you control over timing, for example immediately after a large load. The longer-term fix is to move the table to liquid clustering with CLUSTER BY.