dbt · Beginner
What are snapshots in dbt?
By Anurag Pandey, Lead Data Engineer ·
The short answer
A snapshot records how rows in a changing source table look over time, as a Type 2 slowly changing dimension. dbt adds validity columns such as dbt_valid_from and dbt_valid_to, and detects changes with the timestamp or check strategy.
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.
Say it like this
About 50 seconds out loud. Use it as a model, then put it in your own words.
Snapshots let me keep history for a source table that gets updated in place, like a customers table where the address changes. Each time I run dbt snapshot, dbt compares the current source rows with what it stored before. When a row has changed, dbt closes the old version by setting dbt_valid_to and inserts the new version with a new dbt_valid_from. That's Slowly Changing Dimension Type 2. To detect changes there are two strategies. Timestamp uses an updated_at column, which is the most reliable when the source has one. Check compares a list of columns, for when there's no trustworthy timestamp. Because a snapshot only captures what it sees when it runs, I schedule it often enough not to miss changes, and I read from the source as directly as possible.
What interviewers look for
- You connect snapshots to SCD Type 2.
- You explain the timestamp and check strategies and when to use each.
- You know a snapshot only sees changes that exist when it runs.
Common mistakes
- Treating a snapshot like a normal model you can rebuild. Its history can't be recreated.
- Building snapshots on top of heavily transformed models instead of the raw source.
- Ignoring deletes in the source, which need extra configuration.
Follow-up questions to expect
- 1How do you handle rows deleted from the source?
- 2Why is the timestamp strategy preferred when available?
- 3How would you query the value of a customer's address on a given date?
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.