Skip to main content
A type 2 slowly changing dimension keeps every version of a dimension row instead of overwriting it. In ClickHouse, store the versions in an append-only table and let a materialized view decide at insert time whether an incoming row differs from the current version. Validity intervals do not have to be stored or updated: compute them with a window function, or look up the version that was valid at a given time with ASOF JOIN. If you load data with dbt or Fivetran, they can maintain such a table for you: see dbt snapshots and Fivetran history mode.

Tables and materialized view

How it works:
  • Every load inserts into customers_input. It uses the Null engine, so the rows are not stored there: they only pass through the materialized view.
  • customers_history holds one row per version of a customer. row_hash is a hash of the attributes whose changes you want to track.
  • For every inserted block, the view looks up the latest version of each incoming key. The LEFT JOIN returns default values for a key without history, so last_version = 0 means a new key. The view keeps the rows of new keys and the rows whose hash differs from the hash of the latest version, and gives them the number last_version + 1. Unchanged rows are discarded.
  • Inside a materialized view, the source table stands for the inserted block, also in subqueries. The condition customer_id IN (SELECT customer_id FROM customers_input) therefore limits the lookup to the keys of the block, and the primary key of customers_history is used to read only their history instead of the whole table.
  • SETTINGS join_use_nulls = 0 fixes the behavior of the LEFT JOIN that the view relies on. Without it, an insert from a session with join_use_nulls = 1 silently skips new keys, because last_version is then NULL for them.
  • ReplacingMergeTree with the sorting key (customer_id, version) leaves at most one row per version of a key after merges.
valid_from is taken from the change time that the source provides. If the source has none, use now() AS valid_from in the view to record the load time.

Load data

The first load creates version 1 of each customer:
Loading the same attributes again adds nothing:
A load with one changed customer, one unchanged customer and one new customer adds two rows:
A value that returns to an earlier state is a new version, because the view compares with the latest version only:

Query the history

The current state is the latest version of each key:
The end of a validity interval is the start of the next version. The lead window function computes it at query time, and the current version has no end:
To join facts with the version of the dimension that was valid when the fact happened, use ASOF JOIN. It needs only valid_from, which must be different for every version of a key (see Limitations):

Limitations

  • One writer at a time. The view reads customers_history and then writes to it, and inserts do not lock each other. Two concurrent loads of the same key can both read the same latest version and both store the next number. ReplacingMergeTree then keeps only the row inserted last (after a merge, or immediately with FINAL), so one of the two changes is lost. Run the loads of a dimension one after another.
  • One row per key in an insert. Rows of the same key in one insert all compare with the same stored version and get the same number, and only the last of them is kept. If a batch can contain several changes of a key, send them in separate inserts, oldest first.
  • Changes must arrive in order. The view appends after the latest stored version. A late row with an older updated_at becomes the newest version.
  • Two versions of a key must not have the same valid_from. With DateTime, this happens when a key changes twice within one second. The interval query then gives the earlier version an empty interval, but ASOF JOIN compares only valid_from and can return either of the two versions. If the source can send such changes, store updated_at and valid_from as DateTime64 with the precision of the source, or join with a subquery that keeps only the latest version for each valid_from: (SELECT * FROM customers_history ORDER BY version DESC LIMIT 1 BY customer_id, valid_from) AS c.
  • The tracked attributes are listed twice, in row_hash and in the WHERE clause of the view. Keep the two expressions identical when you add an attribute.
  • Deletions are not recorded. A key that disappears from the source keeps its last version. To track deletions, have the source send a row with a flag column and include that column in the hash.
  • Every insert runs a lookup in customers_history for the keys of the inserted block. Prefer a few large loads to many small inserts.