• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql删除大表的部分数据_MySQL

mysql 搞代码 4年前 (2022-01-09) 21次浏览 已收录 0个评论

gaodaima.com

mysql删除大表的部分数据

好久没写博客。最近项目要上线。下班时间还得陪着老妈。实在没时间更新。

今天有人提了一个问题,

一个表有1亿6000万的数据,有一个自增ID。最大值就是1亿6000万,需要删除大于250万以后的数据,有什么办法可以快速删除?

当时看了一眼数据吓尿了,这么大的数据要删除到什么时候啊,最要命的锁表肿么办

delete是不行了,加索引也别想。mysql上delete加low_priorty,quick,ignore估计也帮助不大

看到mysql文档有一种解决方案:http://dev.mysql.com/doc/refman/5.0/en/delete.html

If you are deleting many rows from a large table, you may exceed the lock table size for an InnoDB table. To avoid this problem, or simply to minimize the time that the table remains locked, the following strategy (which does not use DELETE at all) might be helpful:

Select本文来源gaodaimacom搞#^代%!码&网* the rows not to be deleted into an empty table that has the same structure as the original table:

INSERT INTO t_copy SELECT * FROM t WHERE … ;

Use RENAME TABLE to atomically move the original table out of the way and rename the copy to the original name:

RENAME TABLE t TO t_old, t_copy TO t;

Drop the original table:

DROP TABLE t_old;

E文不好,简单的翻译下:

删除达标上的多行数据时,innodb会超出lock table size的限制,最小化的减少锁表的时间的方案是:

1选择不需要删除的数据,并把它们存在一张相同结构的空表里

2重命名原始表,并给新表命名为原始表的原始表名

3删掉原始表

总结一下就是,当时删除大表的一部分数据时可以使用 见新表,拷贝数据,删除旧表,重命名的方法。

gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql删除大表的部分数据_MySQL

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址