Sub topic 12
Letting Databricks run maintenance for you
This chapter covers predictive optimization, the Databricks feature that schedules table maintenance for you. It assumes you have read the earlier chapters on OPTIMIZE, liquid clustering, optimized writes, and VACUUM. You should also know the basics of Unity Catalog, where tables live in a catalog, a schema, and a table name.
What predictive optimization does
Earlier chapters showed maintenance you run by hand or with a scheduled job: OPTIMIZE to compact files, VACUUM to delete unreferenced files, and statistics collection to keep data skipping effective. Predictive optimization is a service that runs these operations for you on Unity Catalog managed tables.
The service watches how each table is used and written. It then decides which operations are worth running and when. It runs them on serverless compute that Databricks manages. You do not pick a cluster or write a schedule.
It automates these operations:
| Operation | What it does | Related earlier topic |
|---|---|---|
| Compaction | Runs `OPTIMIZE` to merge small files into larger ones | Compacting files with OPTIMIZE |
| Vacuum | Runs `VACUUM` to delete files that are no longer referenced and are past retention | Cleaning up with VACUUM |
| Statistics | Runs `ANALYZE` to collect statistics used by data skipping and the query optimizer | Data skipping and file statistics |
| Clustering | For liquid clustered tables, applies the clustering through `OPTIMIZE` | Liquid clustering |
It does not choose your layout. It does not pick clustering columns or decide between partitioning and liquid clustering. You make that design decision. Predictive optimization keeps the layout you chose in good shape.
Where it applies
Predictive optimization only works on Unity Catalog managed tables. Tables are out of scope in these cases:
- External tables, where you control the storage location. Databricks does not manage their files.
- Tables in the legacy Hive metastore.
- Tables outside Unity Catalog generally.
If you have such tables, keep using your own OPTIMIZE and VACUUM jobs for them.
How it decides what to run
You do not see the exact algorithm. Predictive optimization looks at signals such as how many small files a table has, how much data is unreferenced and ready for VACUUM, and how stale the statistics are. It runs an operation only when the expected benefit justifies the compute cost.
This differs from a fixed nightly OPTIMIZE job in three ways:
- A table that nobody writes to is left alone. A fixed job would rescan it every night.
- A table with heavy ingestion gets attention sooner than a calendar schedule would give it.
- The work is incremental. For clustered tables, only the files that need work are rewritten.
Enabling and disabling it
The setting is hierarchical. It can be set at the account level by an account admin. Below that, you can set it on a catalog or a schema. A schema either inherits from its catalog or overrides it.
-- Turn it on for every managed table in a catalog
ALTER CATALOG main ENABLE PREDICTIVE OPTIMIZATION;
-- Turn it off for one schema that you maintain yourself
ALTER SCHEMA main.staging DISABLE PREDICTIVE OPTIMIZATION;
-- Go back to following the catalog's setting
ALTER SCHEMA main.staging INHERIT PREDICTIVE OPTIMIZATION;You need sufficient privileges on the object, typically ownership, to change the setting. Disabling is useful for scratch schemas, for tables that are about to be dropped, or for tables where you want full control over timing.
To see the current setting, run DESCRIBE CATALOG EXTENDED main or DESCRIBE SCHEMA EXTENDED main.staging. The output includes the predictive optimization property and whether it is inherited.
Verifying what it did
Automation you cannot see is hard to trust. Databricks records each run in a system table, system.storage.predictive_optimization_operations_history. Querying it needs access to the system catalog, which an admin usually grants.
SELECT
start_time,
catalog_name,
schema_name,
table_name,
operation_type,
operation_status,
usage_quantity,
usage_unit
FROM system.storage.predictive_optimization_operations_history
WHERE catalog_name = 'main'
AND start_time >= current_timestamp() - INTERVAL 7 DAYS
ORDER BY start_time DESC;Column names can vary as the system table evolves, so check the schema with DESCRIBE system.storage.predictive_optimization_operations_history if a query fails. The operation_type column tells you which kind of maintenance ran. Typical values cover compaction, vacuum, statistics, and automatic clustering. The usage_quantity column shows what the run cost in DBUs.
You can also confirm the effect on a single table with commands from earlier chapters:
DESCRIBE HISTORY main.sales.events;
DESCRIBE DETAIL main.sales.events;DESCRIBE HISTORY lists OPTIMIZE and VACUUM entries in the Delta log. DESCRIBE DETAIL shows numFiles and sizeInBytes. If compaction ran, numFiles drops and the average file size (sizeInBytes / numFiles) rises.
How it relates to write-time optimization
Predictive optimization does not replace optimized writes and auto compaction. They work at different moments.
- Optimized writes and auto compaction act during or right after a write. They keep each commit from leaving many tiny files.
- Predictive optimization acts later, in the background. It catches what remains, such as files that accumulated across many commits, plus work that only makes sense after the fact, like
VACUUM.
Using both is reasonable. Write-time settings reduce how much small-file debt builds up, and predictive optimization pays the rest down. On managed tables where predictive optimization is on, you often do not need a separate OPTIMIZE job at all.
Costs and trade-offs
Maintenance runs on serverless compute, and you pay for it in DBUs. Databricks reports this usage under its own billing category, and the system table above shows it per run. Predictive optimization is not free.
- Benefit: no cluster to size, no job to schedule, and no forgotten tables that quietly degrade.
- Cost: compute spend you do not directly schedule. Review it periodically, especially for tables with high write churn.
- Control: you cannot force a specific time. For a table that must be compacted right before a known heavy read, such as month-end reporting, run
OPTIMIZEyourself. A manual run does not conflict with the automated one. - Scope: it does not run
ZORDER BY. A Z-ordered table still gets compaction and vacuum, but if you want its layout to follow your query filters automatically, liquid clustering is the supported path. - Retention:
VACUUMuses the table's retention settings. Time travel and readers that need old versions are bound by those settings, so tune them deliberately if you need a longer window.
Common mistakes
- Assuming it covers every table. External tables and Hive metastore tables are skipped. Audit which tables are actually managed.
- Leaving duplicate manual jobs running. A nightly
OPTIMIZEon a table that predictive optimization already maintains wastes compute. Retire the old job after you confirm the system table shows regular runs. - Expecting a layout change. It will not add clustering columns for you. If a table has no
CLUSTER BYand no useful partitioning, compaction alone will not fix poor data skipping. - Never checking the history. If a table still has thousands of small files, look at the system table to see whether the feature is enabled for that schema and whether runs are succeeding.
Exercise
A team has a managed table main.sales.orders in a schema with predictive optimization enabled. It is still slow, and DESCRIBE DETAIL shows numFiles in the tens of thousands. Name two things you would check, and one thing predictive optimization would not fix.
Show answer
First, query system.storage.predictive_optimization_operations_history for this table to see whether compaction runs are happening and whether their status is successful. Second, run DESCRIBE SCHEMA EXTENDED main.sales (and the catalog) to confirm the setting is enabled and not overridden by an explicit DISABLE. Predictive optimization would not fix a poor layout choice: it does not select clustering columns, so if queries filter on columns the table is not clustered on, data skipping stays weak even after compaction.
Exercise
You have an external table in cloud storage that you own, plus a managed table in Unity Catalog. Which one does predictive optimization maintain, and what do you do for the other?
Show answer
It maintains the managed Unity Catalog table. For the external table, keep running your own OPTIMIZE and VACUUM jobs on a schedule (and ANALYZE if you need fresh statistics).