Sub topic 8
Delta file layout and OPTIMIZE
This chapter assumes you know basic SQL, have used Databricks clusters or SQL warehouses, and understand that a Delta table is a set of Parquet data files plus a transaction log. It covers how file layout affects query speed and how OPTIMIZE fixes the most common layout problem.
How a Delta table is stored
A Delta table is a folder of Parquet files and a _delta_log folder. The log records which files belong to the current version of the table. Every write adds new files and updates the log. Deletes and updates do not edit files in place. They write new files and mark the old ones as removed.
A query first reads the log to learn the file list. Spark then plans one or more tasks over those files. The number and size of the files therefore shape the work the query does.
The small files problem
Small files accumulate naturally. Streaming jobs write a little data every trigger. Frequent MERGE and INSERT statements add files with a few rows each. Over-partitioned tables spread data across many tiny folders.
Many small files slow queries in three ways:
- Each file needs a metadata lookup and an open call against cloud storage, which adds fixed overhead per file.
- Spark creates more tasks, and scheduling overhead grows.
- Each file has its own Parquet footer and statistics, so there is less data per read to amortize that cost.
Very large files have the opposite risk. They reduce parallelism and make data skipping coarse. A target in the range of hundreds of megabytes to about 1 GB per file works well for most tables.
Compacting files with OPTIMIZE
OPTIMIZE rewrites many small files into fewer larger ones. It does not change the table's data. It creates a new table version, and the old files stay on storage until VACUUM removes them.
OPTIMIZE orders;Run DESCRIBE DETAIL orders before and after. Compare numFiles and sizeInBytes to see the effect. The OPTIMIZE result also reports how many files it removed and added.
Common errors
- Running OPTIMIZE once and forgetting it. New writes create new small files, so compaction is a recurring task. Later chapters cover automating it.
- Expecting an immediate storage drop. Old files remain until
VACUUMruns past the retention period, so storage can briefly grow. - Optimizing the whole table every time. On a large table, restrict the work with a
WHEREclause on partition columns, for exampleOPTIMIZE orders WHERE order_date >= '2024-01-01'. - Compacting a table that is already healthy. If files are already large,
OPTIMIZErewrites little and costs compute for no gain.
Exercise
A streaming job writes to a Delta table every minute. After a week, queries on the table are slower, and DESCRIBE DETAIL shows 40,000 files averaging 200 KB. What should you run, and why does storage not shrink right away?
Show answer
Run OPTIMIZE on the table to compact the small files into larger ones. Storage does not shrink right away because OPTIMIZE creates a new table version and leaves the old files in place. They are deleted only when VACUUM runs after the retention period.