Two hundred thousand tiny files
A streaming job writes to a table every 10 seconds. After a month, a simple count over one day takes minutes, and the storage listing shows about 200,000 files of a few KB each. Why are reads slow, and how do you fix both the existing data and the job?
Think it through or sketch an answer first, then open the hint or the solution.
Show a hint
Every file has a fixed cost to list, open and read its footer, regardless of size.
Show the solution
Each micro-batch writes at least one file per partition, so small triggers create huge numbers of tiny files, and reads pay per-file overhead. Compact the existing data (on Delta Lake, OPTIMIZE), and change the job: a longer trigger interval, fewer output partitions, and optimised writes or auto compaction where the platform supports them. Aim for files in the hundreds of MB.
Example (SQL)
-- compact existing data (Delta Lake)
OPTIMIZE events WHERE event_date >= '2026-03-01';
-- and write bigger files going forward
ALTER TABLE events SET TBLPROPERTIES (
'delta.autoOptimize.optimizeWrite' = 'true',
'delta.autoOptimize.autoCompact' = 'true'
);
Common mistakes
- Running repartition(1) on a large batch: it removes parallelism and creates one enormous task.
- Compacting once without changing the trigger: the problem returns within days.
How did you do? Your answer is saved in this browser and comes back for revision later.
Learn more
- Table Maintenance: OPTIMIZE, VACUUM and Predictive OptimizationSpark & Databricks
- OPTIMIZE, Z-ORDER & VACUUMDelta Lake
- Structured StreamingSpark & Databricks