缓慢变化维度
模式。 把属性变更流变成 SCD-2 行:每个版本一行,带 effective_ts 与
next_effective_ts,可直接用于时间点查询。
查询
SELECT user, tier, effective_ts,
lead(effective_ts) OVER (PARTITION BY user ORDER BY effective_ts) AS next_effective_ts
FROM flow
lead() 按 key 排列版本;最后一个版本的 next_effective_ts 为 NULL,
表示「仍然生效」。把输出写到以 (user, effective_ts) 为键的 UPSERT sink,
可以在至少一次重投下保持维度表幂等。
输入与输出
输入(用户 a 的两条变更):
| user | tier | effective_ts |
|---|---|---|
| a | silver | 1000 |
| a | gold | 2000 |
输出(SCD-2 行):
| user | tier | effective_ts | next_effective_ts |
|---|---|---|---|
| a | silver | 1000 | 2000 |
| a | gold | 2000 | NULL |
时间点查询
维度物化后,用范围谓词回答「用户 a 在时刻 t 是什么 tier」(参见
时间连接):
SELECT o.*, d.tier
FROM orders o JOIN tiers d
ON o.user = d.user
AND o.ts >= d.effective_ts
AND (d.next_effective_ts IS NULL OR o.ts < d.next_effective_ts)
可运行示例
examples/sql_scd.yaml
(manifest 名称 sql-scd)。
边界
- 批次内有效:
lead()只看到一个批次;同一 key 跨批次的版本需要在下游 合并(UPSERT sink 方案即可:每行有键且幂等)。 - 迟到的旧版本会插入新区间;消费者应把同一 key 的重叠区间视为数据质量信号。