A stream commits to a table in table format Iceberg every minute, and queries slowed 10× — diagnose it
A Spark Structured Streaming job appends events to the Iceberg table lake.events with a one-minute trigger. The table is partitioned by day(event_ts) and event_type (40 values), and each micro-batch runs 40 write tasks, each holding rows of every event type. A week after launch, dashboard queries over one day take ten times longer than at launch, though the daily data volume has not changed.
Constraints: data must stay visible within 15 minutes; the table stays in Iceberg; name the cause and fix both new writes and the data already written.
lake.events.files grouped by partition, top rows (illustrative summary)
event_ts_day event_type data_files avg_size
2026-11-07 click 57 600 1.4 MB
2026-11-07 view 57 600 0.9 MB
2026-11-07 purchase 41 310 0.2 MB
...
snapshots kept: 10 080 manifests in current snapshot: 9 870
one-day dashboard query: planning 48 s, scan 31 sDiagnose the cause and name the fix.
Each per-minute commit writes a file per task per partition — 57,600 files a day per partition, each a request and a footer read, plus manifests to plan over. Commit less often, cluster by partition, run rewrite_data_files, expire snapshots.
- Compacting without expiring snapshots, so the replaced small files stay on storage
- Adding finer partitions, which multiplies the files each commit writes
- Fixing only the Spark task layout and ignoring how often the stream commits
- Why does
rewrite_data_filesfree no storage until the old snapshots are expired? - How would you pick a trigger interval that still meets the 15-minute freshness target?
Cause. Every micro-batch is a commit. It runs 40 tasks, each holding rows of all 40 event types, so a commit writes one file per task into every partition: 40 files per partition a minute, 1,440 commits a day — 57,600 files per partition. Each file is its own storage request and footer read and carries its own column dictionaries, and every commit adds a snapshot and a manifest, so planning walks tens of thousands of manifest entries. The data volume is the same — the object count grew.
New writes. A 10-minute trigger fits the 15-minute freshness target and cuts commits tenfold. Gathering one partition's rows in one task before the write makes a commit write one file per partition instead of 40:
(events
.repartition("event_type") # all rows of a type in one task
.writeStream
.format("iceberg")
.trigger(processingTime="10 minutes")
.option("checkpointLocation", "s3://lake/_chk/events")
.toTable("lake.events"))Files already written. Compaction rewrites them into files of the target size (write.target-file-size-bytes, 512 MB by default), and expiring old snapshots then actually deletes the replaced small files — until then the old snapshots still reference them:
CALL lake.system.rewrite_data_files(table => 'lake.events');
CALL lake.system.rewrite_manifests(table => 'lake.events');
CALL lake.system.expire_snapshots(table => 'lake.events',
older_than => TIMESTAMP '2026-11-07 00:00:00',
retain_last => 100);Compaction and snapshot expiry run on a schedule: the stream keeps committing, and small files come back, if ten times more slowly.