Course contents 08 / 35

08 / 35 · Files and Tables

Dataset layout and compaction

Trace a filter from the whole dataset to one file and calculate what small files cost.

The analyst needs sales for shop 150 on one day. Reading two columns from every file still touches a week of data. A layout can make the date eliminate six days and the shop ID eliminate most of the remaining files before the reader transfers their contents.

Skip parts that cannot match#

A dataset partition groups data by a value or a transform, such as the trading day derived from a timestamp. It gives the planner a way to exclude files using a predicate. This is separate from assigning an owner to a shard in partitioning and rebalancing. A day can have many files that many workers read.

Assume seven days, four 128 MiB files per day and a query for one complete day. The unfiltered set has 7⋅4=287\cdot4=28 files and 28⋅128=358428\cdot128=3584 MiB. If metadata identifies the day of each file, the engine keeps four candidates with 512 MiB of full-file payload. Column projection can cut bytes further. Iceberg calls its mapping from a predicate on a source column to partition values hidden partitioning. A query can filter the timestamp while metadata describes the day transform. A filter cutting through a partition still has to be checked against the rows that were not excluded. (Apache Iceberg 1.10.0, Partitioning)

Arrange ranges so bounds can exclude files#

Suppose the four files for the selected day have shop ID bounds 1–99, 100–199, 200–299 and 300–399. Only the second can contain shop 150. Bounds exclude three more files. They do not prove that the remaining one actually contains 150. The reader still evaluates the predicate. The input falls to one candidate of 128 MiB before projection inside that file.

Figure

Two filters leave one candidate file

Two filters leave one candidate fileFiltering the day leaves four files. Only the second range can contain shop ID 150.Whole week28 files3584 MiBOne day4 files512 MiBshop_id = 1501 file128 MiB
Filtering the day leaves four files. Only the second range can contain shop ID 150.

To get narrow file-level bounds, distribute similar shop IDs to the same files. Sorting each already-mixed file by shop may tighten its row-group or page bounds while leaving its file-wide range broad. Read-time pruning needs metadata at the level where it excludes work. Parquet page indexes are optional and contain statistics and offsets for skipping pages. Iceberg stores partition information and file metrics in manifests. The two levels can eliminate different reads. (Apache Parquet, Page Index, Apache Iceberg, Table Spec)

A finer partition key is useful only when it cuts enough work. Partitioning by exact order ID would create many tiny groups and leave a one-day report visiting a large number of them. Partitioning by day leaves shop filters to within-day layout and statistics. The choice depends on which filters occur together and the size written for each group.

Merge small files without changing the rows#

A batch of tiny files makes the engine pay for opens, metadata and scheduling repeatedly. Suppose one partition holds 1000 MiB in 1000 one-MiB files. Compaction rewrites those rows into eight files of 125 MiB in this toy calculation. Assume payload size stays the same. With a fixed open cost of 4 ms per file, serial open work goes from 1000⋅4=40001000\cdot4=4000 ms to 8⋅4=328\cdot4=32 ms. This is a count of work in a model. Parallel opens, caching and request limits change the elapsed time. Spark models file open cost explicitly when grouping file inputs into tasks. (Apache Spark 4.0.0, Performance Tuning)

Compaction spends read and write I/O to reduce future overhead. Even with the same encoded size, rewriting these files reads 1000 MiB and writes 1000 MiB before metadata work. Some queries may prefer smaller files for parallelism or narrower bounds. Merge within useful groups or cluster by common predicates to avoid broadening the bounds unnecessarily. Iceberg’s maintenance operations include rewriting small data files. (Apache Iceberg 1.10.0, Maintenance)

The replacement also changes which files readers should use. Reading both the old thousand and the new eight duplicates every row. Removing the old set before publishing the new one leaves a gap. The reader needs a complete file set from one committed version.

Next#

Compaction makes a new layout with the same logical rows. Table formats and snapshots explain how to publish the new file set in one change while existing readers keep the old one.

References#

  1. Apache Iceberg. Partitioning, Maintenance and Table Spec. Hidden partition transforms, file metrics and rewriting small files.
  2. Apache Parquet. Page Index. Optional page statistics and offsets for pruning within column chunks.
  3. Apache Spark. Performance Tuning, Spark 4.0.0. Uses an estimated file-open cost to group file inputs for execution.