This query needs to delete over 17 million rows, from a table containing 20 million.
DELETE
FROM statements
WHERE agreement_id IN
(SELECT id
FROM agreements
WHERE created < DATE_SUB(CURDATE(), INTERVAL 6 MONTH));
DELETE
FROM agreements
WHERE created < DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
It takes hours to run, am I missing something that could speed things up a bit?
The subselect by itself takes a few seconds, I don't understand why the delete takes so long.
statementstable, maybe you should makeagreement_idand index.SHOW CREATE TABLE <tablename>for each table referenced in the query. Help us help you — don't make us guess at which data types and indexes you currently have.