Snapshot tables help you keep a history of data changes over time. They let you see what data looked like at different points.
Snapshot tables for historical tracking in dbt
snapshots:
- name: your_snapshot_name
target_schema: your_schema
strategy: timestamp
updated_at: updated_at_column
unique_key: id_column
check_cols: ['column1', 'column2']
source: your_source_tablestrategy: Choose timestamp to track changes based on a timestamp column or check to track changes by comparing columns.
unique_key: This is the column that identifies each record uniquely.
last_updated timestamp.snapshots:
- name: product_price_snapshot
target_schema: analytics
strategy: timestamp
updated_at: last_updated
unique_key: product_id
check_cols: ['price']
source: raw.productssnapshots:
- name: customer_address_snapshot
target_schema: analytics
strategy: check
unique_key: customer_id
check_cols: ['address', 'city', 'zip']
source: raw.customersThis snapshot tracks changes in employee roles by comparing the role column. When you run dbt snapshot, it creates a table that keeps all historical role changes.
version: 2 snapshots: - name: employee_role_snapshot target_schema: analytics strategy: check unique_key: employee_id check_cols: ['role'] source: raw.employees -- Run this snapshot with dbt snapshot command to create and update the snapshot table.
Snapshots require a unique key to identify records.
Choose timestamp strategy if you have a reliable updated timestamp column.
Choose check strategy if you want to track changes by comparing specific columns.
Snapshot tables keep historical versions of data for tracking changes.
Use timestamp or check strategies depending on your data.
Run dbt snapshot to create and update snapshot tables.
Practice
snapshot tables in dbt?Solution
Step 1: Understand snapshot tables
Snapshot tables store copies of data at different points in time to track changes.Step 2: Identify the main use
They are used to save historical versions automatically, not for caching or deleting data.Final Answer:
To save historical versions of data automatically -> Option AQuick Check:
Snapshot tables = historical data tracking [OK]
- Confusing snapshots with caching mechanisms
- Thinking snapshots delete old data
- Assuming snapshots create temporary tables
Solution
Step 1: Recall snapshot config syntax
In dbt snapshots, the strategy is set usingstrategy: timestamporstrategy: check.Step 2: Identify correct keyword
Onlystrategy: timestampis valid; others likemethod,type, ormodeare incorrect.Final Answer:
strategy: timestamp -> Option AQuick Check:
Snapshot strategy keyword = strategy [OK]
- Using wrong keywords like 'method' or 'type'
- Confusing snapshot strategy with model materialization
- Misspelling 'strategy'
snapshots:
- name: customer_snapshot
strategy: check
check_cols: ['email', 'status']What happens when the
email or status changes in the source data?Solution
Step 1: Understand 'check' strategy with check_cols
The 'check' strategy tracks changes in specified columns and creates new versions when they change.Step 2: Apply to given columns
If 'email' or 'status' changes, dbt adds a new record version to keep history.Final Answer:
A new record version is created in the snapshot table -> Option CQuick Check:
Check strategy tracks column changes = new version [OK]
- Thinking existing records get overwritten
- Believing changes are ignored
- Assuming check_cols is not valid syntax
snapshots:
- name: order_snapshot
strategy: timestamp
updated_at: updated_at_columnBut dbt throws an error. What is the likely cause?
Solution
Step 1: Check snapshot config keys for timestamp strategy
For 'timestamp' strategy, the correct key isupdated_at_column, notupdated_at.Step 2: Identify the error cause
Usingupdated_atcauses dbt to error because it expectsupdated_at_column.Final Answer:
The 'updated_at' key should be 'updated_at_column' -> Option DQuick Check:
Timestamp strategy key = updated_at_column [OK]
- Using 'updated_at' instead of 'updated_at_column'
- Misplacing keys outside config block
- Assuming strategy value is wrong
product_id, price, and last_updated. Which snapshot config is best to capture price changes accurately?Solution
Step 1: Understand the difference between timestamp and check strategies
Timestamp strategy uses a datetime column to detect changes; check strategy compares specified columns directly.Step 2: Analyze columns for tracking price changes
Price changes may not always update 'last_updated' reliably, so checking 'price' column directly is safer.Step 3: Choose the best config
Usingstrategy: checkwithcheck_cols: ['price']ensures any price change triggers a new snapshot version.Final Answer:
strategy: check with check_cols: ['price'] -> Option BQuick Check:
Check strategy on price column = accurate price tracking [OK]
- Using timestamp on a non-datetime column
- Relying on last_updated which may be unreliable
- Checking wrong columns for changes
Ready to prove it?
Pass the test and this pattern moves unknown
