1. 程式人生 > >InnoDB引擎,從大表中刪除多行

InnoDB引擎,從大表中刪除多行

int ota man erro delete mysql 最小 following 可能

官方建議:

  • InnoDB Tables

  • 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:

-- 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;

大概意思:

你要detele大表中的多行,則可能導致鎖定的表的數據量太多,innodb_buffer_pool沒有足夠空間來執行。為了避免這個問題,或者為了最小化減少持有表鎖的時間,你還不如創建一張同樣結構的表,把不刪除的數據放裏面,然後重命名原始表,並給新表命名為原始表的原始表名,這樣還快一點

mysql會報錯"ERROR 1206 (HY000): The total number of locks exceeds the lock table size",InnoDB引擎會因需要鎖的行太多而拋出這個錯誤。解決就加大innodb_buffer_pool_size

InnoDB引擎,從大表中刪除多行