【业务系列】面对大表的归档处理

无人久伴 提交于 2020-03-26 23:25:12

3 月,跳不动了?>>>

可以搞个存储过程:

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 时间,效率如何?

面对上面的手工触发器,安全性很低。 下面推荐这款工具:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!