Skip to main content

Slowly Changing Dimensions (SCDs)

Premium

Mastering Slowly Changing Dimensions (SCDs) and advanced dimension design techniques is crucial for creating robust, scalable data warehouses. These concepts are fundamental to tracking historical changes, enabling accurate analysis, and optimizing query performance in large-scale data environments.

This lesson will equip you with the knowledge and practical skills to confidently tackle SCD-related questions and scenarios in your interviews, preparing you for real-world data engineering projects.

The importance of historical data

Understanding the significance of historical data tracking is fundamental to grasping the concept of SCDs. In data warehouse design, maintaining accurate historical information is critical for:

ReasonDescriptionExample
Point-in-time reportingAnalyzing data as it existed at a specific momentCustomer status on a particular date
Trend analysisTracking changes over timeProduct category shifts
Compliance and auditingMeeting regulatory requirementsEmployee role history
Historical contextUnderstanding past decisionsPrevious marketing campaign details

Types of SCDs

SCDs are techniques used to handle changes in dimension attributes over time, preserving historical accuracy for analysis.

Type 0: Retain original

SCD Type 0 is the simplest type, where dimension attributes remain unchanged over time.

Characteristics:

  • Attribute value remains constant
  • No historical values maintained
  • Simplest to implement and query

Use cases:

  • Date of birth
  • Original account creation date
  • Social security number

Best practices:

  • Use for truly unchanging attributes or where only the original value is relevant
  • Clearly document Type 0 attributes to prevent accidental updates.

Customer dimension with Type 0 attributes: Date of Birth, original credit score

customer_iddate_of_birthoriginal_credit_score
10011990-05-15720
10021985-11-30680

Type 1: Overwrite

SCD Type 1 replaces the old attribute values with new ones without preserving history.

Characteristics:

  • Always shows current values.
  • No historical tracking.
  • Simple to implement and query.

Use cases:

  • Correcting data entry errors
  • Updating non-critical information (e.g., phone number, email)

Best practices:

  • Use sparingly to avoid historical inconsistencies.
  • Ideal for attributes where only the current value matters.
  • Be cautious of potential impact on data consistency and analysis.

Customer dimension before and after a Type 1 change for email.

Before:

customer_idnameemail
1001John Doe[email protected]
1002Jane Smith[email protected]

After:

customer_idnameemail
1001John Doe[email protected]
1002Jane Smith[email protected]

Type 2: Add new row

SCD Type 2 is widely used for maintaining a full history of changes by adding new rows.

Characteristics:

  • Preserves a full history of changes.
  • Adds complexity to querying.
  • Increases storage requirements.

Use cases:

  • Tracking changes in critical business attributes (e.g., customer segments, product categories)
  • Maintaining accurate historical reporting

Best practices:

  • Include start_date, end_date, and is_current flag for easier querying
  • Use surrogate keys to uniquely identify each version of a dimension record
  • Ensure fact tables reference the appropriate dimension version based on the date of the fact

Customer dimension before and after a Type 2 change for customer segment.

Before:

customer_idnamecustomer_segmentstart_dateend_dateis_current
1001John DoeSilver2022-01-012022-12-31False
1001John DoeGold2023-01-019999-12-31True
1002Jane SmithBronze2022-01-019999-12-31True

After:

customer_idnamecustomer_segmentstart_dateend_dateis_current
1001John DoeSilver2022-01-012022-12-31False
1001John DoeGold2023-01-019999-12-31True
1002Jane SmithBronze2022-01-012023-12-31False
1002Jane SmithSilver2024-01-019999-12-31True

Implementation:

SQL
-- Close the current record UPDATE dim_customer SET end_date = CURRENT_DATE - INTERVAL '1 day', is_current = FALSE WHERE customer_id = 'C001' AND is_current = TRUE; -- Insert the new record INSERT INTO dim_customer ( customer_id, address, start_date, end_date, is_current ) VALUES ( 'C001', '456 New St', CURRENT_DATE, NULL, TRUE );

Type 3: Add new attribute

SCD Type 3 adds new columns to store previous values, offering limited historical tracking.

Characteristics:

  • Maintains current and one historical value.
  • Limited history (usually just the previous state).
  • Moderate complexity in querying and storage.

Use cases:

  • Tracking one previous state (e.g., previous address, former name).
  • When full history isn't needed, but some historical context is valuable.

Best practices:

  • Limit use to attributes where only one historical change is relevant.
  • Clearly name columns to indicate current and previous values.
  • Consider combining Type 3 with Type 2 for more comprehensive historical tracking.

User dimension before and after Type 3 change for activation status

Before:

user_idnamecurrent_activation_statusprevious_activation_statusactivation_change_date
1001John Doenot_activatedNULLNULL
1002Jane Smithactivatednot_activated2023-03-15

After:

user_idnamecurrent_activation_statusprevious_activation_statusactivation_change_date
1001John Doeactivatednot_activated2023-04-22
1002Jane Smithactivatednot_activated2023-03-15

Implementation:

SQL
ALTER TABLE dim_product ADD COLUMN previous_category VARCHAR(50), ADD COLUMN category_change_date DATE; UPDATE dim_product SET previous_category = current_category, current_category = 'Electronics', category_change_date = CURRENT_DATE WHERE product_id = 'P001';

Choosing the right SCD type

Selecting the appropriate SCD type depends on several factors:

FactorConsideration
Business requirementsNeed for historical reporting and analysis
Change frequencyHow often the attribute changes
Storage constraintsAvailable space and impact on table size
Query patternsCommon analysis and reporting needs

Decision framework:

  1. If the attribute never changes or only the original value matters, use Type 0.
  2. If only the current value is relevant and historical accuracy isn't required, use Type 1.
  3. If full historical tracking is necessary, use Type 2.
  4. If comparison between current and previous state is important, but full history isn't required, use Type 3.

Practical application in interviews

When discussing SCDs in interviews:

  • Demonstrate understanding of different SCD types and their use cases.
  • Explain the trade-offs between historical accuracy and performance/storage implications.
  • Discuss real-world scenarios where you've implemented SCDs and the reasoning behind your choices.
  • Be prepared to design a dimension table that incorporates multiple SCD types for different attributes.

Sample interview question: "Design a customer dimension for an e-commerce platform that tracks changes in customer addresses and maintains the original registration date."

Proposed solution:

Column NameDescriptionSCD Type
customer_keySurrogate primary key
customer_idUnique business key
first_nameFirst nameType 1/2 (depending on tracking needs)
last_nameLast nameType 1/2 (depending on tracking needs)
addressAddressType 2
cityCityType 2
stateStateType 2
zip_codeZIP codeType 2
registration_dateRegistration dateType 0 (never changes)
start_dateRecord start date
end_dateRecord end date
is_currentCurrent status flag

Detailed explanation:

  • Name Fields: The first_name and last_name can use Type 1 SCD if only the most recent values are needed or Type 2 SCD if historical changes need to be tracked.
  • Address-Related Fields: The address-related fields (address, city, state, zip_code) use Type 2 SCD to capture changes over time, preserving the history of changes to address information.
  • Registration Date: The registration_date uses Type 0 SCD as it represents a historical fact that does not change.
  • Dates and Status:
    • start_date and end_date: These fields track the effective date range of each record version, which is crucial for historical tracking.
    • is_current: This flag indicates whether the record is the current version, simplifying queries for the latest data.
  • Surrogate Primary Key: customer_key is a surrogate primary key, uniquely identifying each record in the table.
  • Business Key: customer_id is a unique business key (natural key) that uniquely identifies a customer in the source system.

Mastering Slowly Changing Dimensions is crucial for building robust, historically accurate data warehouses and excelling in data engineering interviews. By understanding the characteristics and best practices for SCD Types 0 through 3, you can make informed decisions about handling changing dimension attributes in various scenarios.

Remember to practice implementing these concepts in your projects and be ready to discuss your experiences and decision-making process in interviews. Your ability to navigate the complexities of SCDs will demonstrate your expertise in dimensional modeling and data warehouse design, setting you apart as a skilled data engineer.