Incremental strategies
An incremental model can add its new batch to the table in five different ways: append, merge, delete+insert, insert_overwrite or microbatch. Each fits a different kind of data and warehouse. This lesson compares them.
Pipelines in production · dbt · Gold · Production incrementals, lesson 1 of 2
On this page
Read first: Incremental models (skip if you know it)
In plain words first
Adding this week's receipts to a ledger: you can write them at the end (append), update any line already there (merge), tear out this week's pages and rewrite them (delete+insert or insert_overwrite), or handle each day as its own small job (microbatch).
Five ways to add a batch
An incremental run always starts the same way: dbt runs your SELECT with is_incremental() true and stores the result in a temporary relation. The incremental_strategy decides how that batch gets into the existing table.
| Strategy | What it runs | Needs |
|---|---|---|
| append | insert into t select * from batch | Nothing. Duplicates are possible. |
| merge | merge into t using batch on t.key = batch.key when matched then update ... when not matched then insert ... | unique_key (without it, behaves like append on most adapters) |
| delete+insert | delete from t where key in (select key from batch); then insert into t select * from batch | unique_key |
| insert_overwrite | Replaces whole partitions: every partition present in the batch is overwritten | partition_by (BigQuery, Spark, Databricks) |
| microbatch | Splits the time range into batches (one per day, say) and replaces each batch's time slice | event_time, batch_size, begin |
| Warehouse | Default | Also supports |
|---|---|---|
| Snowflake | merge | append, delete+insert, insert_overwrite (whole table), microbatch |
| BigQuery | merge | insert_overwrite, microbatch |
| Databricks / Spark | merge (Delta) | append, insert_overwrite, replace_where, microbatch |
| Postgres | append, or delete+insert with a unique_key | merge (Postgres 15+), microbatch |
| Redshift | append, or delete+insert with a unique_key | merge, microbatch |
incremental_strategy explicitly on important modelsmodel: A SELECT statement in a .sql file. dbt runs it and saves the result in the warehouse as a table or view named after the file. Learn more → so behaviour does not change with an upgrade.merge or delete+insert?
merge
merge_update_columns or merge_exclude_columns). Fails if the batch has the same key twice ("duplicate row detected during DML action" on Snowflake).delete+insert
Either way, deduplicate the batch to one row per key. The strategy cannot fix a batch with two versions of the same invoice.
Composite keys
{{ config( materialized='incremental', incremental_strategy='merge', unique_key=['invoice_id', 'line_number'] ) }} select invoice_id, line_number, product_code, quantity, unit_price, modified_at from {{ ref('stg_invoice_lines') }} {% if is_incremental() %} where modified_at >= (select max(modified_at) from {{ this }}) {% endif %} qualify 1 = row_number() over ( partition by invoice_id, line_number order by modified_at desc )
A list makes dbt match on all the columns together (on t.invoice_id = b.invoice_id and t.line_number = b.line_number). The alternative is one surrogate key column. Both work; the important part is that none of the key columns is null, because null = null is not true, so a row with a null key never matches and is inserted again on every run.
Making merges cheaper
A merge has to find matching keys in the whole target table. On a table of billions of rows, that scan dominates. If updates only ever touch recent rows, tell the warehouse:
{{ config(
materialized='incremental',
unique_key='play_id',
incremental_predicates=[
"DBT_INTERNAL_DEST.played_on >= dateadd(day, -7, current_date)"
]
) }}
The predicate is added to the merge condition, so the warehouse can prune old partitions or micro-partitions. The trade-off: a late update older than 7 days no longer matches, and is inserted as a duplicate. Pair the predicate with a matching filter on the batch.
insert_overwrite
On BigQuery and Spark, a table partitioned by date can be updated by replacing whole partitions: no key matching at all. Recompute the last three days, and those three partitions are swapped in. It is idempotentidempotent: Safe to run again: running a step twice gives the same result as running it once. Learn more → by construction (re-running a day gives the same day) and cheap, but the batch must contain every row of each partition it touches: a batch that filters a partition down to a subset deletes the rest.
microbatch (dbt 1.9)
{{ config( materialized='incremental', incremental_strategy='microbatch', event_time='played_at', batch_size='day', begin='2024-01-01', lookback=2 ) }} select play_id, account_id, track_id, played_at from {{ ref('stg_plays') }} -- no is_incremental() block
- You do not write the filter. dbt runs the model once per day in the range and filters each
refby its ownevent_time(configured on the upstream model) to that day. - Each day is replaced as a unit, so re-running a day is safe, and failed days can be retried alone with
dbt retry. lookback=2also reprocesses the two previous days on each run, to pick up late events.- Backfill a range with
dbt run --event-time-start 2025-01-01 --event-time-end 2025-02-01.
Common mistakes
- Leaving the strategy to the adapter default — An adapter upgrade or a move between warehouses changes behaviour. Set it.
- Nullable unique key columns — null never equals null, so those rows are inserted again every run.
- insert_overwrite with a partial partition — Every partition in the batch is replaced by exactly the batch's rows.
Key takeaways
- The strategy decides how the batch reaches the table: append, merge, delete+insert, insert_overwrite or microbatch.
- Defaults differ per adapter; set incremental_strategy explicitly.
- Keys must be non-null and the batch unique per key, whatever the strategy.
- incremental_predicates, insert_overwrite and microbatch avoid scanning the whole target.
Interview prep
THE QUESTION
"Compare dbt's incremental strategies. Which would you use for a large fact table with updates?"
Avoid: "merge is always best". Partition overwrite or microbatch can be cheaper and idempotent, and append is right for immutable events.
Follow-ups · answer out loud, then open
Follow-up"What is DBT_INTERNAL_DEST in incremental_predicates?"
Scenario"A merge on a 4 billion row table takes 40 minutes, though only the last day changes. What do you do?"
Trap"With delete+insert, duplicate keys in the batch cause an error, so they are safe."
Check yourself
3 questionsA merge on Snowflake fails with a duplicate row error. What is the most likely cause?
Show the answer
The new batch has the same unique_key twice. merge cannot match one target row to two source rows.
What does insert_overwrite replace?
Show the answer
Every partition that appears in the batch. Whole partitions are swapped, without key matching.
In a microbatch model, who writes the time filter?
Show the answer
dbt, using event_time on the model and its inputs. dbt filters each batch by event_time automatically.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · Production incrementals · lesson 2 of 2 · 5 min readLate data and idempotent incrementals
Lookback windows, re-runs that give the same answer, schema changes and when to full refresh.
Related lessons
Previous: Unit tests
Primary sources: Incremental strategies · Microbatch