Skip to content
Gold 6 min read · Incremental 3 practice problems ↓

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

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.

StrategyWhat it runsNeeds
appendinsert into t select * from batchNothing. Duplicates are possible.
mergemerge 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+insertdelete from t where key in (select key from batch); then insert into t select * from batchunique_key
insert_overwriteReplaces whole partitions: every partition present in the batch is overwrittenpartition_by (BigQuery, Spark, Databricks)
microbatchSplits the time range into batches (one per day, say) and replaces each batch's time sliceevent_time, batch_size, begin
WarehouseDefaultAlso supports
Snowflakemergeappend, delete+insert, insert_overwrite (whole table), microbatch
BigQuerymergeinsert_overwrite, microbatch
Databricks / Sparkmerge (Delta)append, insert_overwrite, replace_where, microbatch
Postgresappend, or delete+insert with a unique_keymerge (Postgres 15+), microbatch
Redshiftappend, or delete+insert with a unique_keymerge, microbatch
Note: Check your adapter's documentation before relying on this table: defaults and options change between adapter versions. Set 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

One atomic statement. Can update only some columns (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

Two statements in a transaction. Replaces whole rows. Tolerates duplicate keys in the batch silently, which is worse: both rows are inserted. Often faster for large batches on some warehouseswarehouse: A database built for analytics on large tables, like Snowflake, BigQuery, Databricks or Redshift. dbt runs its SQL there. Learn more →.

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

dbt · models/marts/fct_invoice_lines.sql
{{ 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:

dbt · Only scan the last 7 days of the target when matching
{{ 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)

dbt · models/marts/fct_plays.sql
{{ 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 ref by its own event_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=2 also 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?"

append only inserts; merge matches on unique_key and updates or inserts in one statement; delete+insert deletes matching keys then inserts; insert_overwrite replaces whole partitions; microbatch processes fixed time slices independently. For a large fact table with updates I would use merge with a non-null (possibly composite) unique_key, a deduplicated batch, and incremental_predicates limiting the target scan to the window where updates can happen. If the table is date-partitioned and updates are always recent, insert_overwrite or microbatch on the last few days avoids key matching altogether and makes re-runs idempotent.

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?"
The alias dbt gives the target table in the generated merge (the batch is DBT_INTERNAL_SOURCE). A predicate on it limits which part of the target the merge scans.
Scenario"A merge on a 4 billion row table takes 40 minutes, though only the last day changes. What do you do?"
Limit the target scan: incremental_predicates on the target's date (with clustering or partitioning on that column), or switch to insert_overwrite or microbatch by day, which replaces recent partitions without key matching.
Trap"With delete+insert, duplicate keys in the batch cause an error, so they are safe."
delete+insert inserts both duplicates silently. merge is the one that errors. Deduplicate the batch either way.

Check yourself

3 questions

A 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 read
Late 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