The dbt snapshot Command
Once you have defined your snapshot logic in the snapshots/ folder, you need to execute it.
If you type dbt run, dbt will completely ignore your snapshot files. Snapshots are a distinct type of resource in dbt, and they require their own dedicated command.
Topics Covered
- The
dbt snapshotcommand - Node Selection
- Best Practices for Scheduling
1. The dbt snapshot Command
To build your slowly changing dimensions, you must run:
dbt snapshot
What happens the first time you run this?
Because the target table does not exist in Snowflake yet, dbt runs a standard CREATE TABLE command. It copies all the rows from your source table, adds the four dbt metadata columns (dbt_scd_id, dbt_updated_at, dbt_valid_from, dbt_valid_to), and sets the dbt_valid_to to NULL for everyone.
What happens the second time you run this?
dbt runs a complex MERGE statement. It compares the raw data to the snapshot table. If it detects that Arjun's address changed from Mumbai to Delhi, it executes an UPDATE to expire the Mumbai row (setting dbt_valid_to), and an INSERT to create the new Delhi row.
2. Node Selection Syntax
Just like dbt run, the dbt snapshot command supports full node selection syntax.
If you only want to execute a specific snapshot, use the --select flag:
dbt snapshot --select snp_customers
If you want to run all snapshots related to the marketing department, you can select by path:
dbt snapshot --select path:snapshots/marketing
(Note: If you use the unified dbt build command, dbt will automatically run your snapshots at the correct time in the DAG!)
3. Best Practices for Scheduling
Snapshots are highly sensitive to scheduling.
Imagine your source application updates Arjun's row at 9:00 AM (Mumbai), and then updates it again at 2:00 PM (Delhi).
If you only run dbt snapshot once a day at Midnight, dbt will look at the source table at Midnight, see "Delhi", and record the change. It will completely miss the fact that Arjun lived in Mumbai for 5 hours.
The Golden Rule of Snapshots: The frequency of your snapshot schedule determines the granularity of your historical tracking.
- If you run
dbt snapshotdaily, you can only track day-by-day changes. - If you run
dbt snapshothourly, you can track intra-day changes.
If your Indian fintech startup requires strict auditing of exactly when a user's account balance changed, you must run your snapshot command frequently (e.g., every 15 minutes) or use an event-streaming architecture instead of snapshotting.
Summary
- Snapshots are not executed by
dbt run. You must usedbt snapshot(ordbt build). - The first run creates the table; subsequent runs execute a
MERGEto track changes. - The command supports all standard
--selectsyntax. - Run snapshots frequently! If a row changes twice between snapshot runs, the intermediate change is lost forever.