Sub topic 14
Keeping Tables Healthy with OPTIMIZE and VACUUM
This chapter assumes you can read Delta table history, know that every commit adds data files and a log entry, and have used time travel and RESTORE. Both commands here act on files, not rows. They never change what a query returns for the current version of the table.
Why tables need maintenance
Each write adds new Parquet files. Streaming jobs and frequent small appends create many small files, and every query has to open each one. UPDATE, DELETE and MERGE also leave behind old files. Those files are no longer part of the current version, but they stay in storage so time travel can read them.
That gives two separate problems with two separate commands:
| Problem | Command | Effect |
|---|---|---|
| Too many small files slow reads | `OPTIMIZE` | Rewrites small files into larger ones |
| Unreferenced files use storage | `VACUUM` | Deletes files the table no longer references and that are past retention |
Compacting files with OPTIMIZE
OPTIMIZE combines small files into larger ones, and it commits the result as a new table version. Readers on the previous version are not blocked, because the old files remain until VACUUM removes them.
OPTIMIZE main.sales.orders;If the table uses liquid clustering, OPTIMIZE also clusters the data by the clustering columns. This is how the CLUSTER BY (event_date) table from the layout chapter gets its layout applied incrementally.
OPTIMIZE main.sales.events;You can limit the work to part of a partitioned table with a WHERE clause on partition columns. Compaction costs compute, so run it on a schedule that matches how fast small files accumulate, not after every write.
Removing old files with VACUUM
VACUUM deletes data files that are no longer referenced by the current table version and are older than the retention threshold. The default is 7 days.
VACUUM main.sales.orders RETAIN 168 HOURS;168 HOURS equals 7 days, so this statement matches the default. You can preview what would be removed first:
VACUUM main.sales.orders DRY RUN;The trade-off with time travel
Time travel and RESTORE need the old files. After VACUUM removes them, VERSION AS OF for a version that depended on those files fails. So the retention period is the window in which you can recover from a bad write.
- A longer retention keeps more history and costs more storage.
- A shorter retention saves storage and shrinks the recovery window.
- Retention should also exceed the runtime of your longest queries and streams. A reader that started on an old version can fail if
VACUUMdeletes files it still needs.
VACUUM does not delete the transaction log entries themselves. Log retention is a separate table property, so history listed by DESCRIBE HISTORY can outlast the data files it points to.
A maintenance routine
A common pattern runs both commands in order from a scheduled job:
OPTIMIZE main.sales.orders;
VACUUM main.sales.orders RETAIN 168 HOURS;Running OPTIMIZE first is deliberate. It creates the compacted files and makes the small ones unreferenced, so the later VACUUM can remove them once they pass the retention age. Right after OPTIMIZE, the small files are still within retention, so storage may not drop until a later VACUUM run.
Common mistakes
- Expecting
VACUUMto free space immediately. Files younger than the retention period stay. - Running
VACUUMwith a very short retention and then trying toRESTOREto an older version. - Running
OPTIMIZEafter every small write. The compaction cost can exceed the read savings. - Assuming
OPTIMIZEchanges query results. It only changes file layout.
Exercise
A table was changed by a bad DELETE on Monday. On the following Tuesday, VACUUM ... RETAIN 168 HOURS ran, and on Wednesday you try RESTORE TABLE ... TO VERSION AS OF the version from before the delete. Will it work, and why?
Show answer
It most likely fails. The delete happened more than 7 days (168 hours) before the VACUUM ran, so the data files that the earlier version depended on were unreferenced and past retention, and VACUUM deleted them. The log still lists the version, but the files it points to are gone. To keep a recovery window, use a longer retention period.
Exercise
Why is OPTIMIZE usually run before VACUUM in a maintenance job, and why might storage not shrink right after the first VACUUM?
Show answer
OPTIMIZE rewrites small files into larger ones, which leaves the small files unreferenced so VACUUM can remove them. The old small files are only deleted once they are older than the retention period, so storage drops on a later VACUUM run, not immediately.