doushenmao9036 2014-12-15 13:48
浏览 47
已采纳

MySQL在更改条目时优化数据库中的数据存储

We faced with a problem:

When we are generating accounts (or other documents), we use customer (employee) data. However, customers can change their data (e.g. woman get married and her last name changed, or she moved to live in other country and her address changed).

We need, that after editing woman last name, accounts remain with old last name until specific date,and after that specific date, use woman new last name.

e.g. We have a woman: Emily Smith. She married 2014 December 15, and her last name become Brown. Before: December 15 we generate her account with Smith last name but after December 15 we need to generate her account with Brown last name. So in database we need to save both her last names.

How to save entry optimized with only one or a few changed attributes?

P.S. We also need to save the date when the change was made in order to know which last name to use, old or new.

  • 写回答

2条回答 默认 最新

  • dongpian6319 2014-12-15 13:55
    关注

    This is called a slowly changing dimensions. The traditional approach is to have records with an effective date and end date attached. So, Emily would have two rows in this table:

    AccountId    Firstname     LastName     EffDate      EndDate
       1           Emily       Smith        2001-01-01   2014-12-14
       1           Emily       Brown        2014-12-15   NULL
    

    Often, the NULL value for the current record would be some arbitrary date far in the future (such as 9999-12-31). Not having to deal with NULLs in the logic can improve perfromance.

    Then, when you want the record as of a particular date, you just use the record that is valid on that date. If you want the current record, the same logic holds -- find the record valid today.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗