Mysql – How to a MySQL delete statement affect a table with over 30 millions records

deleteMySQL

I have to delete about 28 million records from a InnoDB table in MySQL.
I am wondering what happens if I am doing a select statement on the same table during the delete operation.
I guess the delete will take some time to complete.

I am using MySQL version 5.6.
I don't want to delete all records I will have a where condition or maybe having.
Thank you for your time.

Best Answer

Instead of deleting a large percentage of the rows you might better negate your WHERE-condition and INSERT/SELECT the remaining rows into a new table. Then DROP the original table and RENAME new to old (or TRUNCATE and re-INSERT). This way there's only a very small period (DROP/RENAME) where the table is not available. It's a very common process in a Data Warehouse where you deal with really large numbers of rows.

You need to run both the INSERT/SELECT and the DROP TABLE within a single transaction, so no other session is able to do any DML between the INSER and the DROP. I don't know the exact mySQL/InnoDb syntax and transaction behavior, but this is a skeleton (of course you need to test before):

SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- 
START TRANSACTION;
LOCK TABLES newTab WRITE; 
INSERT INTO newTab SELECT * FROM oldTab; 
DROP TABLE oldTab;  -- might need a COMMIT?;
RENAME newTab TO oldTab;
COMMIT; -- ?