Problem
A customer moves region or a product changes category, and past facts must still be reported under the attributes that were true then. Overwriting dimension rows rewrites history and breaks historical reports.
Also known as: scd, scd-type-2
Track how descriptive attributes of dimension records change over time in an analytical model.
A customer moves region or a product changes category, and past facts must still be reported under the attributes that were true then. Overwriting dimension rows rewrites history and breaks historical reports.
Slowly changing dimensions, from Kimball dimensional modeling, define how a warehouse records changes to descriptive attributes. The most used variant is Type 2, which preserves full history by versioning rows.
Each dimension row has a surrogate key, the business key, tracked attributes, valid_from, valid_to and is_current. On a change, the pipeline expires the old row and inserts a new one.
-- close old version
UPDATE dim_customer SET valid_to = :ts, is_current = false
WHERE customer_id = :id AND is_current AND region <> :new_region;
-- open new version
INSERT INTO dim_customer
(customer_id, region, valid_from, valid_to, is_current)
VALUES (:id, :new_region, :ts, '9999-12-31', true);
When loading facts, look up the surrogate key whose validity range contains the event timestamp. Reports grouped by region then attribute each sale to the region at the time of sale. For current-state views, filter on is_current.
Other types: Type 0 never changes, Type 1 overwrites, Type 3 stores one prior value, Type 4 splits fast-changing attributes into a mini-dimension, Type 6 combines 1, 2 and 3.
Low - modest extra storage
Medium - merge pipelines and tests
Medium - several type variants
Overlapping effective date ranges for one business key
Fact rows point to the wrong surrogate key after late changes
Rapidly changing attribute explodes row count