视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
mysql删除大表的部分数据_MySQL
2020-11-09 18:25:19 责编:小采
文档


bitsCN.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 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删掉原始表

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

bitsCN.com

下载本文
显示全文
专题