Partition count, file sizing, and the small-file problem
Why 200 shuffle partitions is almost always wrong, and how directory layout can destroy a table.
The rule
Target 128 to 256 MB per shuffle and per output file. spark.sql.shuffle.partitions defaults to 200, and that number is wrong for most jobs in both directions: too many for a small job, too few for a large one.
A job shuffling 10 GB into 200 partitions produces 50 MB partitions, comfortably in range. The same setting applied to a 10 TB shuffle produces 50 GB partitions, each one too large to process without spilling.
Why it costs what it costs
Too few, oversized partitions mean each task holds more data than fits comfortably in memory, forcing spills to disk and slower, less parallel execution. Too many, undersized partitions mean per-task scheduling overhead and per-file open and metadata cost start to dominate the actual work being done.
's coalesce feature (spark.sql.adaptive.coalescePartitions.enabled) merges small post-shuffle partitions at runtime based on real observed sizes, which is why hand-tuning the 200 default is mostly obsolete on modern Spark versions. It solves the too-many-partitions side of the problem automatically; it does not fix a static shuffle count that is too low for the data volume.
The small-file problem
Thousands of tiny files mean per-file open cost on every read, bloated file-listing metadata, and slower directory listing overall. In Delta Lake this also bloats the transaction log, since every file is tracked as an entry in the log's metadata.
The fix is (file compaction) or liquid clustering, both of which rewrite many into fewer, properly sized ones.
Partition columns are physical directories
Choosing a partition column means choosing a directory structure on disk. Pick a , high-selectivity column that queries actually filter on, usually a date.
Partitioning by a high-cardinality column (customer_id, for example) creates one directory per distinct value. With millions of customers that is millions of directories, each potentially holding a handful of tiny files. This is the fastest way to destroy a table's read and write performance at once.
A high-cardinality partition column turns the small-file problem into the default behavior of every write.
Comparison: partition column choices
Partition column cardinality
| Column | Cardinality | Effect |
|---|---|---|
| event_date | Low (one value per day) | Good: few large directories, queries filter on it naturally |
| region | Low | Good, if queries commonly filter by region |
| customer_id | Very high | Bad: one directory per customer, small-file problem by construction |
| store_id | Medium | Depends: fine if stores number in the hundreds, risky in the tens of thousands |
Spark UI evidence
Check the number of tasks in the read stage against the expected file count: thousands of tasks reading tiny files shows up as a task count far exceeding the data volume would justify. The SQL tab's scan node shows "number of files read" and total size, letting you compute average file size directly.
Worked example
A target file size changes the work shape
Illustrative compaction with a target near 256 MiB. Both layouts represent the same roughly 1 GiB payload and logical rows.
| Layout | File count | Approx. size per file |
|---|---|---|
| Before OPTIMIZE | 256 | 4 MiB |
| After OPTIMIZE | 4 | 256 MiB |
What to notice: Fewer tiny files reduce listing, open, and scheduling overhead. Storage file size and Spark shuffle partition count are separate controls and should be tuned for their own workloads.
Practice / self-test
1. A job shuffles 10 TB with the default 200 shuffle partitions. What's wrong, and what's the fix?
Reveal answer
200 partitions over 10 TB is 50 GB per partition, far above the 128-256 MB target, so tasks spill heavily. Raise spark.sql.shuffle.partitions to bring each partition back into range, or rely on AQE coalesce only if the issue is too many small partitions, which it is not here.
2. Why does AQE coalesce make manual shuffle-partition tuning mostly unnecessary?
Reveal answer
AQE observes the actual post-shuffle partition sizes at runtime and merges undersized ones automatically, so setting shuffle partitions high up front and letting AQE coalesce down is safer than guessing a fixed number in advance.
3. A table has 2 million files for 500 GB of data. What's the likely root cause and the fix?
Reveal answer
Likely a high-cardinality partition column or many small incremental writes without compaction. Fix with OPTIMIZE or liquid clustering to rewrite the small files into properly sized ones, and reconsider the partition column choice going forward.
4. Why is customer_id a poor partition column choice even if queries do filter on it?
Reveal answer
Its cardinality is extremely high, so partitioning by it creates one directory per customer, almost certainly with small files in each. The small-file problem this creates usually costs more than the pruning benefit gains back.