多版本共存,增量方案设计

需求场景

表father与表son有父子级关系,需要一张表维护father_id与son_id的关系,同时需要保存每次变更的历史,需要满足历史变更查询、根据版本查询关联、版本数据维护等场景

备注:
– data_associated_table 关联关系表
– id: 主键
– del_flag: 删除标识(用来表示取消关系,为了历史变更做逻辑删除,1删除,0生效)
– father_id: 父级数据主键(#{fatherId}为mybatis的参数)
– son_id: 子级数据主键(#{sonId}为mybatis的参数)
– version: 版本号

  1. 新增
    查询出最新的版本+1,增量保存
    insert into data_associated_table(id, del_flag, son_id, father_id, version)
    select replace(uuid_short(), ‘-’, ‘’), 0, #{sonId}, #{fatherId}, max(a.version) + 1
    from ent_industry_code a;
  2. 删除
    删除整个版本:删除版本号为查询版本的所有记录
    delete from data_associated_table where version = #{version};
  3. 修改
    取消关联:和新增逻辑类似,del_flag标识为1
  4. 查询
    查询出所有小于或等于查询版本的记录,可分开查询,等于查询版本+小于查询版本(需要排除等于的)
    select b.*
    from data_associated_table b
    inner join (
    select a.father_id, a.son_id, max(a.version) as max_version
    from data_associated_table a
    where a.version <= #{version}
    group by a.father_id, a.son_id
    ) c
    on b.father_id = c.father_id
    and b.son_id = c.son_id
    and b.version = c.max_version
    where b.del_flag = 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值