Skip to content
Silver 5 min read · Incremental 2 practice problems ↓

Incremental models

Rebuilding a billion-row table every night to add a million new rows wastes time and money. An incremental model processes only what is new. This lesson shows how is_incremental() works, the first-run trap, and when to rebuild in full.

Pipelines in production · dbt · Silver · Change over time, lesson 1 of 2

Read first: Materializations (skip if you know it)

In plain words first

Updating a photo album, you add the new photos at the back; you do not reprint the whole album each week. Once in a while, if pages got mixed up, you rebuild it from scratch.

An incremental modelmodel: 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 → is the weekly addition; --full-refresh is the rebuild.

Why incremental

A table model rebuilds everything every run. For a billion-row plays table that grows by a million rows a day, that is a thousand times more work than needed. An incremental model processes only the rows that are new since the last run and adds them to the existing table.

The shape of an incremental model

dbt · models/marts/fct_plays.sql
{{ config(materialized='incremental') }}

select
    play_id,
    account_id,
    track_id,
    played_at
from {{ ref('stg_plays') }}

{% if is_incremental() %}
  -- only rows newer than what the table already has
  where played_at > (select max(played_at) from {{ this }})
{% endif %}

Run 1 · first build

The table does not exist yet, so is_incremental() is false. The filter is left out and dbt runs create table fct_plays as (select ... from stg_plays): the full history.

Run 2 · next day

The table exists, so is_incremental() is true. {{ this }} compiles to the table itself, and the filter keeps only plays after its current maximum. dbt puts those rows in a temporary table and inserts them.

Full refresh · --full-refresh

dbt run --select fct_plays --full-refresh makes is_incremental() false again: the table is rebuilt from scratch. Needed after a logic change that should apply to old rows too.

is_incremental() is true only when all of these hold: the model is materializedmaterialization: How dbt stores a model: as a view, a table, an incremental table that only adds new rows, or ephemeral (inlined, not stored). Learn more → incremental, its table already exists, it is a table (not a view), and the run is not a full refresh.

{{ this }}

{{ this }} is the relation the model itself builds, here analytics.dbt_alice.fct_plays. You use it to read the model's own current state: its max timestamp, its existing keys. Never ref() a model from inside itself: that is a cycle.

unique_key: from append to upsert

Without a key, new rows are only appended. If a sourcesource: A raw table loaded by another tool, declared in YAML so models can select from it with source() and dbt can test it and check its freshness. Learn more → row can change (an invoice goes from open to paid), appending the new version leaves two rows for one invoice. Setting unique_key tells dbt to replace existing rows with the same key:

dbt · models/marts/fct_invoices.sql
{{ config(
    materialized='incremental',
    unique_key='invoice_id'
) }}

select invoice_id, account_id, status, total, modified_at
from {{ ref('stg_invoices') }}

{% if is_incremental() %}
  where modified_at > (select max(modified_at) from {{ this }})
{% endif %}

Table before the run + new rows

invoice_idstatusmodified_at
1open03-01
2open03-01
2 (new)paid03-02
3 (new)open03-02

Table after the run

invoice_idstatusmodified_at
1open03-01
2paid03-02
3open03-02

Invoice 2 is replaced, invoice 3 is inserted, invoice 1 is untouched.

How the replacement happens depends on the incremental strategy (merge, delete+insert and others, covered at level 3). The new batch itself must have one row per key: if invoice 2 changed twice since the last run and both versions are in the batch, a merge fails or inserts both. Deduplicate the batch first.

Writing the filter

  • Use >=, not >. Several rows can share the latest timestamp, and some of them may arrive after the last run. With > they are skipped forever. >= picks up that timestamp again; the rows already loaded come back too, but with a unique_key they replace themselves instead of duplicating.
  • Filter on a column that only grows, such as a load timestamp, when event times can arrive late (see Late data).
  • Keep the subquery on {{ this }} simple: most warehouseswarehouse: A database built for analytics on large tables, like Snowflake, BigQuery, Databricks or Redshift. dbt runs its SQL there. Learn more → compute max() from metadata quickly.
Watch out: Changing the model's SQL changes only new rows. Old rows keep the old logic until a --full-refresh. If a column is added, existing rows have no value for it, and by default dbt ignores the new column on incremental runs (see on_schema_change).

Common mistakes

  • Forgetting the first run — Without the is_incremental() guard, the filter queries {{ this }} before it exists.
  • Duplicate keys in the new batch — A merge on a key that appears twice in the batch errors or inserts duplicates. Deduplicate first.
  • Changing logic without a full refresh — Old rows keep the old logic; the table becomes a mix.

Key takeaways

  • An incremental model builds everything once, then only new rows.
  • is_incremental() is false on the first run and on --full-refresh; {{ this }} is the model's own table.
  • unique_key replaces existing rows with the same key instead of appending duplicates.
  • Keep each batch unique per key, and full-refresh after logic changes.

Interview prep

THE QUESTION

"Walk me through how a dbt incremental model works."

On the first run, or with --full-refresh, the table does not exist so is_incremental() is false: dbt builds the full table. On later runs is_incremental() is true, so the filter inside {% if is_incremental() %} limits the source to new rows, usually by comparing a timestamp to (select max(ts) from {{ this }}), where this is the model's own table. dbt writes that batch to a temp table and inserts it, or with unique_key upserts it, replacing rows with matching keys using the adapter's strategy (merge or delete+insert). The batch must be unique per key, and logic changes need a full refresh to reach old rows.

Avoid: "incremental models only process new data, so they are always correct". Late data, duplicates in the batch and logic changes all break them.

Follow-ups · answer out loud, then open

Follow-up"What is {{ this }} and why not ref() the model itself?"
{{ this }} is the relation the model builds, used to read its own current state. ref() to itself would be a cycle.
Scenario"An incremental model with unique_key has duplicate keys in the table. How?"
Usually the batch contained the same key twice (with delete+insert both are inserted), a key column was null (null never matches), or the model ran without unique_key at some point. Deduplicate the batch, make the key non-null, add a unique test, and full-refresh to clean up.
Trap"After fixing a bug in an incremental model, the next run fixes the table."
Only rows in new batches get the new logic. Old rows keep the bug until a full refresh or a backfill.

Check yourself

3 questions

When is is_incremental() false?

Show the answer

On the first run and with --full-refresh. It needs an existing table and a non-full-refresh run.

What does {{ this }} refer to in an incremental model?

Show the answer

The model's own existing table. this is the relation the model builds.

Without unique_key, a changed invoice arrives again in a new batch. What happens?

Show the answer

It is appended, so the invoice now has two rows. Without a key, incremental runs only append.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · Change over time · lesson 2 of 2 · 5 min read
Snapshots (SCD Type 2)
Record how mutable source rows change over time with the timestamp and check strategies.

Related lessons

Previous: Packages and dbt_utils

Primary sources: Incremental models · is_incremental