Mysql保留历史数据_如何优化MySQL的更改历史数据

您需要一个缓慢变化的维度:

我将只为电子邮件和电话这样做,所以你理解(注意我使用两个键的事实,1表在表中是唯一的,另一个是它所关心的用户所独有的。这是,表键标识记录,用户键标识用户):

table_id,user_id,email,telephone,created_at,inactive_at,is_current

1,1,mario @ yahoo.it,123456,2012-01-02 ,,2013-04-01,no

2,2,erik @ telecom.de,123457,2012-01-03,2013-02-28,no

3,3,vanessa @ o2.de,1234568,2012-01-03,null,yes

4,2,erik @ telecom.de,123459,2012-02-28,null,yes

5,1,super.mario @ yahoo.it,654321,2013-04-01,2013-04-02,no

6,1,super.mario @ yahoo.it,123456,2013-04-02,null,yes

数据库的最新状态

select * from FooTable where inactive_at is null

select * from FooTable where is_current = 'yes'

对马里奥的所有更改(马里奥是user_id 1)

select * from FooTable where user_id = 1;

2013年1月1日至2013年5月1日之间的所有变更

select * from FooTable where created_at between '2013-01-01' and '2013-05-01';

你需要与旧版本进行比较(借助于存储过程,java或php代码...你选择了)

select * from FooTable where incative_at between '2013-01-01' and '2013-05-01';

如果你想要你可以做一个花哨的sql语句

select f1.table_id, f1.user_id,

case when f1.email = f2.email then 'NO_CHANGE' else concat(f1.email , ' -> ', f2.email) end,

case when f1.phone = f2.phone then 'NO_CHANGE' else concat(f1.phone , ' -> ', f2.phone) end

from FooTable f1 inner join FooTable f2

on(f1.user_id = f2.user_id)

where f2.created_at in

(select max(f3.created_at) from Footable f3 where f3.user_id = f1.user_id

and f3.created_at < f1.created_at and f1.user_id=f3.user_id)

and f1.created_at between '2013-01-01' and '2013-05-01' ;

正如您可以看到一个多汁的查询,将user_与预览用户行进行比较......

2013-03-01上的数据库状态

select * from FooTable where table_id in

(select max(table_id) from FooTable where inactive_at <= '2013-03-01' group by user_id

union

select id from FooTable where inactive_at is null group by user_id having count(table_id) =1 );

我认为这是实现你想要的最简单的方法......你可以实现一个数百万的表关系模型,但是查询它会很麻烦

你的数据库不够大,我每天都用一个更大的数据库工作。现在告诉我你在一台新服务器上存下的钱是否值得花时间在超复杂的关系模型上?

BTW如果数据变化太快,则无法使用此方法......

奖励:优化:

在created_at,inactive_at,user_id和对上创建索引

执行分区(水平和垂直)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值