Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 87/ 160
05. Data Lakehouses & Open Table Formats

Delta Lake Operations (MERGE INTO / Upserts, OPTIMIZE, Z-Ordering, VACUUM)

Upsert with MERGE, compact with OPTIMIZE/Z-ORDER, and VACUUM old files safely.

AdvancedMERGE · OPTIMIZE · VACUUM3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

One final source row per merge key. Next, prune unrelated target partitions. Finally, validate readers/time travel, then remove old files.

Think of it like…

MERGE is the inventory amendment desk, OPTIMIZE repacks shelves, Z-ORDER groups commonly requested labels, and VACUUM discards old crates only after every auditor is finished with that receipt.

Where you’ve seen it

MERGE INTO orders t USING staged s ON t.tenant_id=s.tenant_id AND t.order_id=s.order_id WHEN MATCHED ...; OPTIMIZE orders WHERE dt='2026-11-21' ZORDER BY (customer_id) then vacuum with default retention or a consciously shorter tested window.

Words you’ll hear

MERGE
Delta SQL for key-matched upsert, delete, or conditional transformation as a transactional multi-file operation.
OPTIMIZE
A Delta rewrite that compacts data files, optionally with Z-Order locality for selected columns.
VACUUM
A metadata-aware cleanup that physically deletes obsolete table files older than a configured retention period.
Operational boundary
Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers. Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

Visual diagram

Loading interactive diagram

Theory & deep dive

MERGE: the working mechanism

MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention.

One final source row per merge key. Prune unrelated target partitions, then compact target file sizes and optional locality; this exposes the state governed by VACUUM.

Delta Lake Operations in one concrete design

MERGE INTO orders t USING staged s ON t.tenant_id=s.tenant_id AND t.order_id=s.order_id WHEN MATCHED ...; OPTIMIZE orders WHERE dt='2026-11-21' ZORDER BY (customer_id) then vacuum with default retention or a consciously shorter tested window.

Key takeaway: MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention

Delta Lake Operations: failure mode and production response

Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.

Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons. Track Merge scan as "source and target files touched by the match predicate", and make Z-Order columns visible to the dataset owner.

Key takeaway: Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

How it works, step by step

Step 1 of 4
MERGE

1. Stage and dedupe

One final source row per merge key. MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention.

one final source row per merge key
Key numbers at this step
Merge scan
source and target files touched by the match predicate
OPTIMIZE
A Delta rewrite that compacts data files, optionally with Z-Order locality for selected columns.

When to use it & trade-offs

What it is

MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention. MERGE INTO orders t USING staged s ON t.tenant_id=s.tenant_id AND t.order_id=s.order_id WHEN MATCHED ...; OPTIMIZE orders WHERE dt='2026-11-21' ZORDER BY (customer_id) then vacuum with default retention or a consciously shorter tested window.

Why it matters: Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers. Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

When to reach for it

  • Upsert with MERGE, compact with OPTIMIZE/Z-ORDER, and VACUUM old files safely.
  • Choose MERGE because it provides this capability: Delta SQL for key-matched upsert, delete, or conditional transformation as a transactional multi-file operation.
  • Keep evidence for Merge scan at "source and target files touched by the match predicate" so the last production stage-Vacuum - validate readers/time travel, then remove old files-remains testable.

When to avoid or be careful

  • Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.
  • Do not conflate OPTIMIZE with MERGE. The second concept contributes different behavior: A Delta rewrite that compacts data files, optionally with Z-Order locality for selected columns.
  • Release only after this guardrail is in place: Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

MERGE / OPTIMIZE / VACUUM - roles, limits, and operating cost

MERGE
Best for: Delta SQL for key-matched upsert, delete, or conditional transformation as a transactional multi-file operation.

Strengths: Delta SQL for key-matched upsert, delete, or conditional transformation as a transactional multi-file operation. MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention.

Watch out: Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.

OPTIMIZE
Best for: A Delta rewrite that compacts data files, optionally with Z-Order locality for selected columns.

Strengths: A Delta rewrite that compacts data files, optionally with Z-Order locality for selected columns. In this workflow it supports: Optimize - compact target file sizes and optional locality.

Watch out: The operating cost is real: usually one to three high-value correlated keys.

VACUUM
Best for: A metadata-aware cleanup that physically deletes obsolete table files older than a configured retention period.

Strengths: A metadata-aware cleanup that physically deletes obsolete table files older than a configured retention period. Its output can be checked with VACUUM retention.

Watch out: Operating guardrail: Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for MERGE, and how would Merge scan reveal it?”
  • “Why is OPTIMIZE distinct from this behavior: a metadata-aware cleanup that physically deletes obsolete table files older than a configured retention period?”
  • Production clue: Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.
  • Design probe: Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

Benchmarks & rules of thumb

Merge scan -
source and target files touched by the match predicate
Z-Order columns -
usually one to three high-value correlated keys
VACUUM retention -
default 7 days in many Delta configs; shorten only with explicit recovery trade-off

Common mistakes & follow-ups

Common interview mistakes

  • Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.
  • Reporting success from a job exit code without checking Merge scan and Z-Order columns.
  • Treating this example as complete without replay, rollback, or access boundaries: MERGE INTO orders t USING staged s ON t.tenant_id=s.tenant_id AND t.order_id=s.order_id WHEN MATCHED ...; OPTIMIZE orders WHERE dt='2026-11-21' ZORDER BY (customer_id) then vacuum with default retention or a consciously shorter tested window.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Delta SQL for key-matched upsert, delete, or conditional transformation as a transactional multi-file operation.
  • What does the recovery plan do when this occurs? Merging a tiny change table can rewrite huge partitions, Z-Ordering too many columns loses locality, and VACUUM run below a safe retention threshold can break time travel or active readers.
  • When is MERGE insufficient for Z-Order columns, and what does adding OPTIMIZE cost in Merge scan?
Interview tip: Lead with the data path and failure boundary: MERGE INTO applies source-keyed INSERT/UPDATE/DELETE actions, OPTIMIZE rewrites files to efficient bins, Z-ORDER co-locates selected correlated values, and VACUUM removes obsolete unreferenced files after retention. Then make the answer operational: Partition and prefilter MERGE by a prunable date/tenant key, compact only hot partitions, choose one to three skew-aware Z-ORDER columns, and align VACUUM/transaction-log retention with active and stream reader horizons.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Delta Lake Operations (MERGE INTO / Upserts, OPTIMIZE, Z-Ordering, VACUUM) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Delta Lake Architecture (Transaction Log _delta_log, ACID Guarantees)
Next topic
Apache Iceberg Architecture (Catalogs, Metadata Files, Manifest Lists, Manifest Files)