Notes · Spark and Iceberg
An Iceberg MERGE that read 930 million rows to write 316 thousand
One of our hourly loads took 14 to 20 minutes to commit a single file of about 316,000 rows. The table it writes to has around 930 million rows, and it turned out the MERGE was reading all of them every hour.
The merge
The target table is partitioned by year, month and day, and the merge condition matches on those columns:
MERGE INTO lake.tags t
USING batch s
ON t.resource_id = s.resource_id AND t.tag_key = s.tag_key
AND t.year = s.year AND t.month = s.month AND t.day = s.day
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
I'd read that condition plenty of times and assumed it limited the merge to the batch's days. It doesn't, at least not reliably. t.year = s.year is a join condition. Spark can use it to prune target partitions only at runtime, through dynamic partition pruning, and only when the batch is small enough to broadcast. Most of our merges have small batches that get broadcast and prune fine. The tags batch was too big, so its plan scanned the whole table.
Putting the dates in the condition
Every row in a batch comes from the dates the job is processing, because the batch is built from that list. So the job can write those dates into the condition as literals on the target side:
def merge_condition(keys, dates):
years = sorted({d.year for d in dates})
months = sorted({d.month for d in dates})
days = sorted({d.day for d in dates})
prune = (f"t.year IN ({','.join(map(str, years))}) "
f"AND t.month IN ({','.join(map(str, months))}) "
f"AND t.day IN ({','.join(map(str, days))})")
return prune + " AND " + " AND ".join(f"t.{k} = s.{k}" for k in keys)
Spark treats literals on the target like any other filter and pushes them into the Iceberg scan while planning. The tags merge plan now shows filters=year = 2026, month = 10, day = 30.
Separate IN lists do over-include near a month boundary. October 31 and November 1 become month IN (10, 11) AND day IN (1, 31), which also matches October 1. I left it that way. The equality conditions still decide which rows match, so the only cost is reading an extra partition or two now and then. When no dates are passed, the condition is unchanged.
Checking it changed nothing else
Changing a merge condition on tables people query every day made me nervous, so I ran the old and new conditions side by side on a local Iceberg catalog with the real merge code, schemas and update maps. Across all 16 datasets that go through this merge the results were identical, and only the new condition pruned during planning. I also ran the catch-up cases that tend to break date logic: hours missed earlier in the day, late rows from the day before, month and year boundaries, and a month-long backfill. On a 43-million-row table with a batch too big to broadcast, the merge went from 19.3 seconds to 2.1.
The other merge jobs write to tables of 59 million rows or fewer, with batches that do get broadcast, so I left those alone.
Since then, whenever a merge or a join is slow, the first thing I look at is the scan node in the physical plan. If the partition columns aren't among the pushed filters, pruning is happening at runtime or not at all, and runtime pruning depends on the batch staying small enough to broadcast. That can change on its own as a source grows. Literal filters don't have that problem, but they're only safe when you can show every batch row belongs to those partitions. If you can't, they'll quietly skip rows you meant to update.
More notes at ankitkhare.dev. Questions or corrections: ankit@ankitkhare.dev.