100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Data Warehouse & Analytics Engineering
30 minadvanced

Snapshots for SCD Type 2 in dbt

dbt Snapshots automate SCD Type 2 dimension table management — tracking attribute changes over time with `dbt_valid_from`, `dbt_valid_to`, and `dbt_is_current` columns, without writing any custom ETL code. A snapshot is defined in the `snapshots/` directory with a `snapshot` Jinja block. On the first run, dbt loads the full source table into the snapshot table. On subsequent runs, dbt compares the current source state to the previous snapshot, identifies changed rows, expires the old version, and inserts the new version automatically.

dbt supports two snapshot strategies. The `timestamp` strategy detects changes by comparing a source `updated_at` column to the last snapshot value — any row where the timestamp has advanced is treated as a change. The `check` strategy detects changes by comparing specified columns (`check_cols`) to their previous values using a hash comparison — appropriate when the source lacks a reliable `updated_at` timestamp. The `timestamp` strategy is more efficient (reads only the timestamp column) and is preferred when the source provides a reliable update timestamp.

Analogy🏏Cricket
🏏 Think of it like cricket: OLTP is the IPL's live ticketing counter — it handles thousands of simultaneous seat reservations, each requiring a precise single-seat record update with immediate confirmation. Speed per transaction and data consistency under concurrent updates are everything. OLAP is the IPL's season statistics department — it runs complex analytical queries across every ball bowled in every match of every season to produce the published rankings, economy rates, and historical comparisons. No one books a seat through the statistics department, and no broadcaster calls the ticketing counter for Bumrah's career economy rate. The two workloads demand completely different systems. Just as the ticketing counter is built for speed and correctness on one seat at a time and would buckle if asked to tally a decade of attendance mid-sale, an OLTP row-store excels at single-record writes but chokes on full-table aggregation; and just as the statistics department pores over millions of past deliveries but would be hopeless at booking a live seat under contention, the OLAP columnar engine sweeps billions of rows yet is the wrong tool for a fast single-row update. The physical design of each — row-oriented for the counter, columnar for the stats desk — is what makes it superb at its own job and unfit for the other's.
Lesson 14 of 35
0% complete