dbt Analytics Engineering (50 Qs)•Part 2: Advanced Modeling, Incremental Builds & Macros (Mid-Level)
How do Incremental Models work in dbt and what are the strategies?
junior LevelCode ExampledbtAdvanced Modeling
In Simple Words:
Rebuilding a 2-billion-row fact table from scratch every hour wastes compute and time.
An Incremental Model builds as a full table on the first run, but on subsequent runs processes only new or updated rows using the is_incremental() macro.
sql
{{ config(
materialized = 'incremental',
unique_key = 'order_id',
incremental_strategy = 'merge'
) }}
SELECT *
FROM {{ ref('stg_orders') }}
{% if is_incremental() %}
-- Only look at events newer than the max timestamp in the existing table
WHERE order_updated_at > (SELECT MAX(order_updated_at) FROM {{ this }})
{% endif %}
Incremental Strategies:
merge(Snowflake, BigQuery, Postgres): Executes an atomic SQLMERGEusingunique_key. Updates existing rows and inserts new rows.delete+insert: Deletes matching keys from the target table and inserts the new rows.append: Appends rows without checking keys (ideal for immutable logs).insert_overwrite(BigQuery/Databricks): Replaces entire partitions (e.g. today's partition) without scanning the rest of the table.