When packing ruins clustering, and defragmentation
How can a size-driven compaction make clustering worse, and is defragmentation still needed?
The interaction: size-driven packing can ruin clustering
Size-driven bin-packing picks files that are adjacent in time (they were written close together). Clustering wants files adjacent in key. If you pack blindly, you merge files from disjoint key ranges into one file with a huge range. Overlap depth goes up, and pruning is worse after compaction than before.
Small example. Two small files: A holds customer_id 1-10, and B holds 9,000-9,010. They are neighbors in time. A blind compaction makes one file with range 1-9,010. A query for customer 500 could skip both files before; now it must read the merged one. The merge made the result worse.
The target selection must be jointly size-aware and key-aware. This is why a naive "compact small files" job sometimes makes queries slower and nobody can explain why.
Defragmentation
On a local disk, defragmentation reorders blocks to reduce seek distance. On object storage this is largely vestigial: there is no in-place file, no block allocator and no seek penalty. What survives under the name is either:
- row-group misalignment inside files (a row group is one internal chunk of a Parquet file); compaction handles it, or
- logical spread of a key across many files; clustering handles it.
Page-level bloat from MVCC version chains (the old row versions that databases keep so that concurrent readers see a consistent view) is real, but it belongs to the storage engine of a database, not to table-file maintenance. The cleanest approach is to fold this item into the two above.