Sub topic 2
Why many small files hurt performance
This chapter assumes you can write basic SQL, have used a Databricks notebook or SQL warehouse, and know that Parquet is a columnar file format. It also assumes you know that Spark splits work into tasks over partitions of data. It covers why small files slow reads, how they pile up, and how to spot them. The fix, OPTIMIZE, comes in the next chapter.
The cost of each file
A Delta table is a set of Parquet data files plus a transaction log that lists which files belong to the current version. Every query starts by reading the log to get that file list. Spark then plans tasks over those files.
Each file adds fixed work that does not depend on how much data it holds:
- The log must track it, so the file list grows and planning takes longer.
- Spark must open it, read its footer, and fetch its statistics from cloud storage.
- Spark may schedule a task for it. A task that reads a few kilobytes spends most of its time on setup instead of reading data.
- Cloud storage charges latency per request, so thousands of small reads cost much more than a few large ones.
A 10 GB table in 100 files of about 100 MB each reads far faster than the same 10 GB in 100,000 files of about 100 KB each. The bytes are the same. The per-file overhead is 1,000 times larger.
How small files accumulate
Small files usually come from how data is written, not from the data itself:
- Streaming and micro-batch writes. Each trigger commits a new file, or several. A job that runs every few seconds creates thousands of files a day.
- Frequent small appends. An hourly job that adds a few megabytes writes a tiny file each run.
- Over-partitioned writes. If a write touches many partition values, each Spark task writes a separate file for each value it holds. A write with 200 tasks and 365 date values can produce up to 73,000 files.
- MERGE, UPDATE, and DELETE. These rewrite only the affected files, and can leave small remnants behind.
Delta never edits a Parquet file in place. Each commit adds new files, so the file count only grows until you compact.
Diagnosing the problem
Start with DESCRIBE DETAIL. It returns one row of metadata about the table, including numFiles and sizeInBytes.
DESCRIBE DETAIL events;Divide sizeInBytes by numFiles to get the average file size. As a rough guide, files in the tens to hundreds of megabytes are healthy for most tables. An average of a few megabytes or less on a large table is a warning sign. The right target depends on table size, so treat these numbers as a starting point and not a rule.
For example, suppose DESCRIBE DETAIL reports numFiles = 80000 and sizeInBytes = 40 GB. The average file is about 0.5 MB. That table is a strong candidate for compaction.
DESCRIBE HISTORY helps explain why. It lists each commit with its operation and metrics, such as the number of files added.
DESCRIBE HISTORY events;Look for many WRITE, STREAMING UPDATE, or MERGE entries that each add only a few files with small sizes. That pattern points to the writer that needs attention.
Common mistakes
- Checking only the row count. A table with few rows can still have thousands of files. Check
numFiles. - Assuming a slow query means a bad query. The same SQL can speed up a lot after the files are compacted.
- Compacting once and stopping. If the writer still produces small files, the problem returns. Fix the writer or schedule maintenance.
Exercise
DESCRIBE DETAIL on a table shows numFiles = 50000 and sizeInBytes = 25 GB. What is the approximate average file size, and which command would you run to find out which writes created the small files?
Show answer
About 0.5 MB per file (25 GB divided by 50,000 files, roughly 500 KB). Run DESCRIBE HISTORY on the table to see each commit's operation and the number of files it added, then look for frequent small writes such as streaming updates or small appends.