在數據倉庫的數據模型設計過程中,經常會遇到這樣的需求:
- 數據量比較大;
- 表中的部分字段會被update,如用戶的地址,產品的描述信息,訂單的狀態等等;
- 需要查看某一個時間點或者時間段的歷史快照信息,比如,查看某一個訂單在歷史某一個時間點的狀態,比如,查看某一個用戶在過去某一段時間內,更新過幾次等等;
- 變化的比例和頻率不是很大,比如,總共有1000萬的會員,每天新增和發生變化的有10萬左右;如果對這邊表每天都保留一份全量,那麼每次全量中會保存很多不變的信息,對存儲是極大的浪費;
- 綜上所述:引入’拉鍊歷史表’,既能滿足反應數據的歷史狀態,又可以最大程度的節省存儲;
select f.id,
f.start_date,
case when s.id is not null and f.end_date > s.update_date then 'yesterday' else f.end_date end as end_date
from fdm_table f
left join sdm_table s
on f.id = s.id
//新的狀態階段
union all select *,
update_date as start_date,
'9999-12-31' as end_date
from sdm_table
//where time_create = 'yesterday'
- https://www.cnblogs.com/zuifangxiu/articles/6475179.html
- https://www.jianshu.com/p/799252156379
- http://pylyria.com/2018/07/03/%E6%8B%89%E9%93%BE%E8%A1%A8%E5%8F%8A%E5%85%B6Hive%E5%AE%9E%E7%8E%B0/