Data Science

What are the best practices for managing Slowly Changing Dimensions (SCD Type 2) in Snowflake?

DA Asked by David Wilson · 14-02-2024
0 upvotes 11,726 views 0 comments
The question

I need to track the history of our "Customer" attributes, specifically their subscription tier and home address. I’m planning to use SCD Type 2 with Start_Date, End_Date, and a Current_Flag. However, our updates are coming in via a high-frequency stream. Is it better to handle this via standard SQL MERGE statements in Snowflake, or should I be looking at "Streams and Tasks" to automate the versioning of these records?

 

3 answers

0
MI
Answered on 17-02-2024

For high-frequency updates in Snowflake, using "Streams and Tasks" combined with the MERGE command is the most robust approach. A Snowflake "Stream" will track the DML changes (inserts/updates) on your landing table. You can then schedule a "Task" to run every few minutes that executes a MERGE statement. The logic should first "expire" the old record by setting the End_Date to current_timestamp and Current_Flag to 'N', and then "insert" the new record with the updated attributes and a null End_Date. This pattern is highly efficient because Snowflake only processes the "change data" rather than scanning the entire table, which saves on credit consumption and keeps your dimension tables up to date in near real-time. 

0
ST
Answered on 20-02-2024

Have you considered the storage overhead of SCD Type 2 if your customer attributes change very frequently, like every few days?

BR 23-02-2024

Steven, that is a concern. We have about 1 million customers, and if 10% change their status weekly, the table will grow quite fast. However, business requirements dictate that we must be able to report on "What the customer's status was at the time of the sale," so we can't just use Type 1. Is there a middle ground? Maybe I should only track "Critical" attributes in Type 2 and move the "Volatile" ones into a separate "Satellite" table to keep the main dimension table lean? I'm trying to optimize for query speed while still meeting the auditor's historical requirements.

0
KI
Answered on 25-02-2024

We use the "dbt-snapshot" feature for all our SCD Type 2 logic. It handles the dates and flags automatically so we don't have to write complex MERGE scripts. 

DA 28-02-2024

dbt-snapshots are a game changer! It makes the whole process declarative. We migrated all our manual SQL scripts to dbt last year and haven't looked back.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session