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
- Every load inserts into
customers_input. It uses theNullengine, so the rows are not stored there: they only pass through the materialized view. customers_historyholds one row per version of a customer.row_hashis 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 JOINreturns default values for a key without history, solast_version = 0means 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 numberlast_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 ofcustomers_historyis used to read only their history instead of the whole table. SETTINGS join_use_nulls = 0fixes the behavior of theLEFT JOINthat the view relies on. Without it, an insert from a session withjoin_use_nulls = 1silently skips new keys, becauselast_versionis thenNULLfor them.ReplacingMergeTreewith 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:Query the history
The current state is the latest version of each key:lead window function computes it at query time, and the current version has no end:
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_historyand 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.ReplacingMergeTreethen keeps only the row inserted last (after a merge, or immediately withFINAL), 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_atbecomes the newest version. - Two versions of a key must not have the same
valid_from. WithDateTime, this happens when a key changes twice within one second. The interval query then gives the earlier version an empty interval, butASOF JOINcompares onlyvalid_fromand can return either of the two versions. If the source can send such changes, storeupdated_atandvalid_fromasDateTime64with the precision of the source, or join with a subquery that keeps only the latest version for eachvalid_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_hashand in theWHEREclause 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_historyfor the keys of the inserted block. Prefer a few large loads to many small inserts.