Sub topic 2
Why many small files hurt performance
This chapter assumes you know basic SQL, have used Databricks notebooks or SQL warehouses, and understand that Parquet is a columnar file format. You should also know that a Spark job is split into tasks, and that each task processes a slice of the data. The previous chapter covered how a Delta table is stored as Parquet data files plus a transaction log. Here we look at what goes wrong when that table holds too many small files.
What counts as a small file
A Delta table is a directory of Parquet files. The transaction log records which of those files belong to the current version of the table. Every write adds new files, and every file costs something to read, whatever its size.
There is no single threshold for "small". As a rough guide, files well under about 128 MB are candidates for compaction. Databricks targets file sizes between 256 MB and 1 GB for OPTIMIZE output, depending on table size and workload. A table of 10 GB that holds 20,000 files averages about 0.5 MB per file. That is a clear small-files problem.
The exact target sizes vary with the runtime version and table settings, so treat the numbers above as orientation, not as fixed rules.
Where small files come from
Small files are a side effect of normal write patterns. The most common sources are:
- Streaming ingestion. Each micro-batch writes at least one file. A stream that triggers every few seconds produces thousands of small files per day.
- Frequent small appends. A job that appends a few hundred rows every few minutes adds a file each time.
- Over-partitioned tables. Each write touches many partition directories, and each directory gets its own small file.
- High shuffle parallelism. A write after a shuffle with 200 partitions can produce up to 200 files, even when the data is only a few megabytes.
- MERGE, UPDATE, and DELETE. These operations rewrite the files they touch. Many scattered changes produce many new, often small, files.
Why small files hurt
Small files slow down both reads and metadata handling. The cost comes from several separate mechanisms.
Per-file overhead on reads
To read a Parquet file, Spark must open it, read the footer, parse the schema and row group metadata, and then schedule a task. These steps take roughly the same time for a 1 MB file as for a 500 MB file. When the table is made of many tiny files, that fixed cost dominates and the actual data scan is a small part of the total.
On cloud object storage, each file also means at least one separate request. Object stores have higher per-request latency than local disks, so thousands of requests add up quickly.
Task scheduling overhead
Spark packs small files into read tasks, but it cannot pack them without limit. A scan over 50,000 tiny files creates many more tasks than a scan over 500 well-sized files. Each task has startup and scheduling cost. The driver must track all of them, and the job's stage can spend more time launching tasks than processing rows.
Larger transaction log and slower planning
Each file in the table has an entry in the transaction log. Before a query runs, Delta must work out the current list of files by reading the latest checkpoint and the JSON commits after it. More files mean larger checkpoints and more work for query planning. The effect appears even for queries that finally read only a small part of the table.
Weaker data skipping
Delta stores minimum and maximum values for columns in each file, and it uses them to skip files that cannot match a filter. When the table has many small files, the statistics are still correct, but the engine has to evaluate far more entries. Small files also tend to hold a random mix of values, so their ranges overlap and fewer files can be skipped. The next chapter covers data skipping in detail.
Poor compression and encoding
Parquet compresses data per column chunk. A small file has few rows per column chunk, so dictionary encoding and run-length encoding work less well. The same data takes more bytes when it is split across many small files than when it sits in a few large ones.
The diagram below shows the same data stored two ways and what a query must do in each case.
The opposite problem: files that are too large
Compaction can overshoot. Very large files reduce parallelism, because one task reads one file or a portion of it. They also make data skipping coarser, since the min and max statistics cover more rows. And a MERGE or UPDATE that touches one row must rewrite the whole file. Aim for large enough to amortize overhead, but not so large that you lose parallelism and cheap updates.
Diagnosing the problem with DESCRIBE DETAIL
The fastest check is DESCRIBE DETAIL. It returns one row of metadata about the table, including numFiles and sizeInBytes. Dividing the second by the first gives the average file size.
DESCRIBE DETAIL events;Read the output columns numFiles, sizeInBytes, and partitionColumns. You can also compute the average directly in a query:
SELECT
numFiles,
sizeInBytes,
ROUND(sizeInBytes / numFiles / 1024 / 1024, 2) AS avg_file_mb
FROM (DESCRIBE DETAIL events);If avg_file_mb is a few megabytes or less on a table of meaningful size, the table has a small-files problem. For a partitioned table, remember that DESCRIBE DETAIL reports the whole table. A table can look healthy on average while one busy partition holds thousands of tiny files. Check recent partitions separately if your queries target them.
Finding the cause with DESCRIBE HISTORY
DESCRIBE HISTORY lists the table's commits. It shows which operation created each version, along with operation metrics such as the number of files added and removed.
DESCRIBE HISTORY events;Look at the operation and operationMetrics columns. Patterns to look for:
| Pattern in history | Likely cause |
|---|---|
| Many `STREAMING UPDATE` commits, each adding a few files | Frequent micro-batches |
| Many `WRITE` commits with small `numOutputBytes` | Frequent small appends |
| `MERGE` commits that add many files and remove many files | Scattered updates rewriting files |
| `WRITE` commits adding hundreds of files with tiny sizes | Too many shuffle partitions or too many partitions in the table |
Compare numFiles or numAddedFiles against numOutputBytes in each commit. A commit that writes 5 MB across 200 files tells you where the problem starts.
Common mistakes
- Judging by file count alone. A table with 5,000 files of 512 MB each is fine. Use average size, not count.
- Compacting a table that is rarely read. Compaction costs compute. If nothing queries the table, the cost buys nothing.
- Reducing parallelism by hand and forgetting about it. Calling
coalesce(1)before a write gives one file per write, but it makes that write slow and can still leave small files across many writes. - Ignoring the cause. Compacting a table once does not stop a streaming job from creating new small files tomorrow. Later chapters cover compaction and write-time settings that address the source.
Putting it together
A practical diagnosis follows three steps:
- Run
DESCRIBE DETAILand compute the average file size. - Run
DESCRIBE HISTORYand find which operations add the most small files. - Check whether your queries target recent data or the whole table, so you know where compaction will pay off.
The next chapters build on this. Data skipping shows why file layout matters beyond file size. OPTIMIZE then gives you the main tool for fixing the problem.
Exercise
A table has numFiles = 24,000 and sizeInBytes = 12,000,000,000 (about 12 GB). It receives a streaming append every 10 seconds. What is the approximate average file size, and which two things would you check next to confirm and understand the problem?
Show answer
The average is 12,000,000,000 / 24,000 = 500,000 bytes, about 0.5 MB per file. That is far below a healthy size, so the table has a small-files problem. Next, run DESCRIBE HISTORY to confirm that many STREAMING UPDATE commits each add a few small files, which explains the cause. Then check whether queries read recent partitions or the whole table, to see where compaction will help most.
Exercise
Name three separate ways many small files slow down a query, and explain why compacting everything into a single huge file is not a good fix.
Show answer
Three ways: (1) each file adds fixed open and footer-read cost plus an object store request; (2) more files mean more tasks to schedule and a larger file list in the transaction log to plan over; (3) small files compress poorly and have overlapping min/max statistics, which weakens data skipping. A single huge file removes parallelism because one task reads it, makes data skipping coarse, and forces a MERGE or UPDATE to rewrite the entire file.