Delta Lake MERGE without the full-table rewrite
Why MERGE quietly rewrites far more data than it changes, and the three fixes that keep it fast: prune the target, deduplicate the source, compact after.
TL;DR
- A
MERGErewrites every file that contains any matching row — one changed row can rewrite a 1 GB file. - Add a partition or file-pruning predicate on the target side of the
ONclause so Delta only touches relevant files. - Deduplicate the source first:
MERGEfails on duplicate matches and wastes effort even when it doesn't. - Compact small files after frequent merges, or the next
MERGEpays for your file fragmentation.
Why MERGE gets slow
MERGE INTO is a join followed by a rewrite. Delta finds every target file containing at least one row that matches the source, then rewrites those files in full. On a wide table with large files, a trickle of updates can turn into a rewrite of most of the table.
At NorthBridge Retail, a nightly merge of ~40,000 changed orders into a 900 GB silver.orders table was rewriting 300+ GB per run. The changes touched recent orders only, but the ON clause gave Delta no way to know that.
Prune the target
Give the optimizer a static predicate on the target so it can skip files before the join:
-- Environment: Databricks Runtime 15.x LTS, Delta Lake
MERGE INTO silver.orders AS t
USING bronze_updates AS s
ON t.order_id = s.order_id
AND t.order_date >= current_date() - INTERVAL 45 DAYS -- prune!
WHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;The extra condition must be written on the target column (t.order_date), and it must be safe: if a late update can arrive for a 60-day-old order, a 45-day window silently drops it. Pick the window from your real late-arrival distribution, not from hope.
Check the plan. Run
EXPLAINon the MERGE and look forPartitionFiltersor file-skipping stats. If the target scan reads the whole table, your predicate isn't pruning.
Deduplicate the source
If the source has two rows for the same key, MERGE throws
DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW. Keep only the latest change per key before merging:
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY order_id
ORDER BY updated_at DESC
) AS rn
FROM bronze_updates
)
SELECT * FROM ranked WHERE rn = 1Steps that matter in production:
- Dedupe on the business key, ordered by the event timestamp, not the ingestion timestamp.
- Break ties deterministically (add a file sequence or offset column to the
ORDER BY). - Do it in the same job as the MERGE so no one can skip it.
Compaction after MERGE
Frequent small merges leave behind small files, and the next MERGE has to open all of them. Schedule compaction:
OPTIMIZE silver.orders WHERE order_date >= current_date() - INTERVAL 45 DAYS;On DBR 13.3+, enable deletion vectors so updates mark rows instead of rewriting whole files:
ALTER TABLE silver.orders
SET TBLPROPERTIES ('delta.enableDeletionVectors' = 'true');When not to use this
If your "updates" are really full daily snapshots, skip MERGE entirely — overwrite the affected partitions with replaceWhere. MERGE earns its cost only when changes are a small fraction of the table.
Checklist
-
ONclause carries a static pruning predicate on the target - Source deduplicated on business key + event time, with a deterministic tie-breaker
-
EXPLAINconfirms file skipping - Compaction scheduled; deletion vectors enabled where DBR allows
- Late-arrival window validated against real data, not assumed
Practice
Further reading
Keep going
Build an SCD Type 2 with MERGE
Apply a day's customer address changes to a dimension table, closing old versions and inserting new ones, in a single MERGE.
MERGE needs its own schema evolution switch
Delta's mergeSchema option does nothing for MERGE statements — automatic schema evolution for MERGE is a separate session setting.
Medallion Architecture Checklist
A printable checklist for designing and reviewing bronze, silver and gold layers — naming, quality gates, ownership and the questions to ask at each hop.
One practical note a week
Get new articles, problems and guides in your inbox.
Thanks! You're on the list. (Local demo — no email provider is wired up yet.)