This guide explores dbt Snapshots as a powerful tool for implementing Slowly Changing Dimensions Type 2 in Snowflake, enabling organizations to capture and preserve historical data changes over time.
There are many powerful tools you can use in conjunction with Snowflake, but today let's explore the powerful tool in dbt, Snapshots, and how to implement it in Snowflake.
Slowly Changing Dimensions (SCDs) play a vital role in preserving historical information while accommodating updates and changes. It refers to the dimensions within a warehouse that changes slowly over time. SCD Type 2 provides a mechanism to capture and store historical changes, enabling analytical queries to accurately reflect the state of dimensions at any given time.
You can add configurations to your snapshot using the config block. In the config block, you can customize the behavior and settings of the snapshot. It provides a way to define parameters and options specific to the snapshot model.
The configurations that are available for the snapshot are:
Dbt Snapshots in Snowflake allow you to capture and manage point-in-time views of your data as it progresses through your data pipeline. This feature enables you to preserve historical information while performing incremental updates to your curated layer.
Start by setting up a dbt project and configuring it to connect to your Snowflake account. Provide the necessary credentials and connection details.
Create a snapshot model for a mutable data source. Here we created a snapshot model for each table in the raw layer to keep track of all the changes happening over time. The snapshot model is created within the snapshot folder. Make sure you mention the file extension in the file name.
For each snapshot, define the config as per the project requirement. Here we have defined the config as per our project requirements like the corresponding target_database, target_schema, unique_key, and strategy. As we are loading the data from the raw layer to the curated layer, we have mentioned the target_database and target_schema as per the curated layer, and in the SELECT statement, we have mentioned the database and schema in the raw layer in Snowflake.
Once the snapshot model is created, you can run it using dbt snapshot so that it will create a snapshot table. In our case, as you can see below when we run the dbt snapshot, all the snapshot tables are created in the curated layer in Snowflake which is the target_database and target_schema we mentioned in the config.
Also in dbt, if you want to run any specific snapshot model or any model you can mention that specific model name like "dbt snapshot –select <model_name>" and you can run that model separately instead of running all the models again using the "dbt snapshot" command.
You can configure dbt to run on a scheduled basis, ensuring that your curated layer is kept up-to-date. This can be achieved using dbt cloud, a scheduler, or custom orchestration tools. In dbt, you can create a Job and run it on a scheduled basis.
Implementing dbt Snapshots in Snowflake provides a robust solution for managing data. By following the step-by-step process outlined above, you can leverage the power of dbt and Snowflake to maintain historical views of your data.