Deleting many rows from a large InnoDB table

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, it is better to do the following way:

  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;
No other sessions can access the tables involved while RENAME TABLE executes, so the rename operation is not subject to concurrency problems. 

Featured Post

NGINX Unit is dynamically configured using a REST API

There is no static configuration file. All configuration changes happen directly in memory. Configuration changes take effect without requir...