【业务系列】面对大表的归档处理
可以搞个存储过程:
create table history_tmp like history;maxid=select max(id) from history;
minid=select id from history where addtime>"2013-01-01 00:00" order by addtime asc limit 1;
last=0;
set autocommit=1;
for(i=minid;i<maxid+1000;i+=1000)
{
insert into history_tmp select * from history where id>=last and id<i lock in share mode;
last=i;
}
begin;
lock table history_tmp write,history write;
maxid=select max(id) from history;
insert into history_tmp select * from history where id>=last and id<=maxid;
alter table history rename to history_2012;
alter table history_tmp rename to history;
unlock tables;
commit;
缺点:
- insert into history_tmp select * from history where id>=last and id<=maxid; 可能会超时吗?
- insert into history_tmp select * from history where id>=last and id<=maxid; 如果不是按Pk 索引,是一些过期的券的dealine 时间,效率如何?
面对上面的手工触发器,安全性很低。
下面推荐这款工具:
- Percona pt-archiver重构版--大表数据归档工具
希望能帮到你节省磁盘成本。
- Mydumper
以上是 【业务系列】面对大表的归档处理 的全部内容, 来源链接: utcz.com/z/532821.html