Snapshot tables for historical tracking in dbt - Time & Space Complexity
When using snapshot tables in dbt, we want to know how the time to update snapshots changes as data grows.
We ask: How does the work grow when there are more records to track?
Analyze the time complexity of the following dbt snapshot configuration.
snapshot:
name: customer_snapshot
strategy: timestamp
updated_at: updated_at
unique_key: customer_id
select * from source.customers
This snapshot tracks changes in the customers table by checking the updated_at timestamp for each record.
Look for repeated work done during snapshot updates.
- Primary operation: Comparing each source record's timestamp to the snapshot's stored timestamp.
- How many times: Once for every record in the source table each time the snapshot runs.
As the number of records grows, the snapshot process checks each record's timestamp.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 timestamp checks |
| 100 | 100 timestamp checks |
| 1000 | 1000 timestamp checks |
Pattern observation: The work grows directly with the number of records.
Time Complexity: O(n)
This means the time to update the snapshot grows in a straight line as the number of records increases.
[X] Wrong: "Snapshot updates only check changed records, so time stays constant no matter how many records exist."
[OK] Correct: The snapshot must scan all records to find which ones changed, so time grows with total records, not just changed ones.
Understanding how snapshot updates scale helps you explain data freshness and performance in real projects.
What if we changed the snapshot strategy from timestamp to check columns? How would the time complexity change?
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
