Sub topic 9
Optimized writes and auto compaction
This chapter assumes you know how OPTIMIZE compacts files and why many small files slow reads. It covers two table features that reduce small files at write time, so you rely less on scheduled OPTIMIZE jobs.
Why fix files at write time
OPTIMIZE repairs small files after the fact. It rewrites data, so it costs compute, and the table stays fragmented until the next run. Two features prevent part of the problem:
- Optimized writes reshuffle data before it is written, so each write produces fewer, larger files.
- Auto compaction runs a small compaction after a write finishes, if the table has too many small files.
Both are Delta table properties. You can turn on one or both.
Enable both features
Set the properties with ALTER TABLE:
ALTER TABLE events SET TBLPROPERTIES (
'delta.autoOptimize.optimizeWrite' = 'true',
'delta.autoOptimize.autoCompact' = 'true'
);After this, writes to events use both features. The properties live in the table metadata, so every writer that uses the table gets the behavior.
How optimized writes work
Without optimized writes, each Spark task writes its own files. A job with 200 tasks writing into a partitioned table can create up to 200 files per partition value it touches. Many of these files are tiny.
With optimized writes, Spark adds a shuffle before the write. The shuffle groups rows by the target partition, so each partition receives a few well-sized files instead of many fragments.
The trade-off is that the extra shuffle adds latency to each write. It pays off most when:
- A write touches many partitions.
- Streaming or frequent micro-batch jobs write small amounts of data.
MERGE,UPDATE, orDELETEstatements rewrite many small pieces.
How auto compaction works
Auto compaction checks the table after a write commits. If a partition holds enough small files, it runs a compaction on that partition. It uses a smaller target file size than a full OPTIMIZE, and it runs synchronously on the cluster that did the write.
The flow looks like this:
Compared with OPTIMIZE
| Aspect | Optimized writes and auto compaction | Scheduled OPTIMIZE |
|---|---|---|
| When it runs | During or right after each write | On a schedule you set |
| Target file size | Smaller | Larger |
| Z-ordering or clustering | No | Yes, with `ZORDER BY` or liquid clustering |
| Cost | Added to each write | Separate job |
Auto compaction does not replace OPTIMIZE. It keeps file counts in check between runs. It does not apply Z-ordering, so if you rely on data skipping from a Z-order or clustering layout, you still need OPTIMIZE.
Common errors
- Expecting auto compaction to Z-order. It only merges small files.
- Enabling it on tables with rare, large batch writes. The files are already large, so the extra shuffle gains little.
- Forgetting VACUUM. Compaction leaves the old small files on storage until
VACUUMremoves them after the retention period. - Checking too early. Run
DESCRIBE DETAIL eventsafter a few writes and comparenumFilesandsizeInBytesto see the effect.
Check the result
Run DESCRIBE DETAIL before and after a batch of writes. Divide sizeInBytes by numFiles to get the average file size. If the average stays far below your target, the features are not catching all the writes, and a scheduled OPTIMIZE is still needed.
Exercise
A streaming job appends small batches to a table every minute. Which feature creates fewer small files during the write, and which one cleans up files after the write commits? Does either one apply Z-ordering?
Show answer
Optimized writes reduce small files during the write by shuffling data into larger files. Auto compaction merges small files after the commit. Neither applies Z-ordering, which needs OPTIMIZE ... ZORDER BY or liquid clustering.