Sub topic 12
Letting Databricks Run Maintenance for You
This chapter assumes you can run OPTIMIZE and VACUUM by hand, and that you know what optimized writes and auto compaction do. It covers how predictive optimization automates that work for Unity Catalog managed tables, and where it falls short.
What predictive optimization does
Predictive optimization is a Databricks feature that decides when to run maintenance on a table and runs it for you. It works on Unity Catalog managed tables. It can run:
OPTIMIZE, to compact small files. On liquid clustered tables this also applies the clustering.VACUUM, to delete data files that are no longer referenced and are past the retention period.ANALYZE, to refresh table statistics used by the query planner.
You no longer schedule these jobs yourself. Databricks looks at how each table is used and written, then runs an operation only when it expects the table to benefit. The work runs on serverless compute that Databricks manages, so you do not size a cluster for it.
How it relates to what you already know
Predictive optimization does not replace the table properties from the previous chapters. It sits on top of them.
| Layer | When it acts | Who triggers it |
|---|---|---|
| Optimized writes | During each write | Table property |
| Auto compaction | Right after a write finishes | Table property |
| `OPTIMIZE` | On demand or on a schedule | You, or predictive optimization |
| `VACUUM` | On demand or on a schedule | You, or predictive optimization |
Write-time settings keep each write from creating too many small files. Predictive optimization handles the larger, periodic jobs that remain.
Turning it on
You can enable predictive optimization for an account, a catalog, or a schema. Lower levels inherit the setting unless they override it. A common choice is to enable it on a catalog and let its schemas and tables inherit it.
ALTER CATALOG main ENABLE PREDICTIVE OPTIMIZATION;
ALTER SCHEMA main.sales DISABLE PREDICTIVE OPTIMIZATION;
ALTER SCHEMA main.sales INHERIT PREDICTIVE OPTIMIZATION;The first statement turns it on for every schema in the catalog. The second opts one schema out. The third returns that schema to the inherited setting. You need the right privileges on the catalog or schema, and your workspace must be in a region where the feature is available. Check the current Databricks documentation for both, because availability changes.
Checking what it did
Automation is only useful if you can see its effect. Use the tools from the monitoring chapter:
DESCRIBE HISTORY main.sales.orders;
DESCRIBE DETAIL main.sales.orders;DESCRIBE HISTORY lists OPTIMIZE and VACUUM operations with their metrics. Operations run by predictive optimization appear alongside the ones you ran yourself. DESCRIBE DETAIL shows numFiles and sizeInBytes, so you can confirm that average file size is healthy. Databricks also exposes a system table that records predictive optimization operations. Its name and columns can change, so confirm them in the documentation before you build a dashboard on it.
Common errors and limits
- Expecting it on external tables. Predictive optimization covers Unity Catalog managed tables. External tables and tables outside Unity Catalog still need your own
OPTIMIZEandVACUUMjobs. - Assuming it picks your layout. It runs maintenance on the layout you chose. It does not choose between partitioning, Z-ordering, and liquid clustering, and it does not pick clustering columns.
- Expecting instant results. It runs when Databricks judges it worthwhile, not right after each write. A table can briefly have small files between runs.
- Dropping write-time settings. Optimized writes and auto compaction still reduce small files at the source, so keep them where they fit.
- Forgetting retention. Automated
VACUUMstill respects the retention period. Time travel and long-running readers depend on that window.
Exercise
A team has an external Delta table with thousands of small files, and predictive optimization is enabled on its catalog. The files are not being compacted. Why, and what should they do?
Show answer
Predictive optimization only runs on Unity Catalog managed tables, so the external table is not covered. The team should run OPTIMIZE (and VACUUM) themselves on a schedule, or convert the data to a managed table so predictive optimization can take over.