[mysql]清除单表大量数据方法(需保留部分数据)

下面的是实现方法:

  1. 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 ... ; //把要删除大量数据的表中不需要删除的数据转存到另外一张表中
  2. 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;//同时重明明两张表,间接实现数据删除操作
  3. Drop the original table:

    DROP TABLE t_old; //视情况而定吧,我清理数据是因为业务慢了,但是既然转存之后不会影响业务也就没删除转存出来的数据。

整个清理数据的过程我用了大概一分钟左右,如果delete的话。。。你懂的

猜你喜欢

转载自blog.csdn.net/yyongsheng/article/details/82938683