0

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.

6
  • Please take a look at this advice
    – mustaccio
    Commented Jan 10, 2019 at 20:16
  • 1
    I'm voting to close this question as off-topic because it lacks sufficient detail required to facilitate a comprehensive answer. Please see dba.meta.stackexchange.com/questions/2990/… for more details. Add the required details, and we'll likely re-open the question.
    – Hannah Vernon
    Commented Jan 10, 2019 at 20:50
  • 1
    You'll probably need a larger innodb_log_file_size - see dev.mysql.com/doc/refman/5.7/en/innodb-redo-log.html. A large innodb_buffer_size is also recommended.
    – danblack
    Commented Jan 11, 2019 at 0:44
  • 4
    You better move 3 million rows into a new table and delete the old one.
    – McNets
    Commented Jan 11, 2019 at 9:33
  • 1
    Here are multiple techniques to deal with big deletes. Deleting more than a thousand rows at a time is costly because of the undo log.
    – Rick James
    Commented Jan 12, 2019 at 2:06

1 Answer 1

1

Use Multiple-Table Syntax of DELETE statement.

DELETE s.*, a.*
FROM statements s, agreements a
WHERE s.agreement_id = a.id
  AND a.created < CURDATE() - INTERVAL 6 MONTH
1
  • I am not sure if it helps with the issue I had, but totally happy with the awesome tip you threw in. Karma is the least I could do. Thanks bud. Commented Jan 20, 2019 at 5:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.