Problem:
I have 2 rather large tables: message
(+20 million records ~ 11GB) and message_subject
(14 million records ~ 3GB) that need to be 'cleaned up'.
I need to delete all messages older than 2 weeks along with the corresponding message_subjects if the message has a subject, but it's important to note that not all messages have a subject and messages older than 2 weeks without a subject need to be removed too.
These 2 tables do NOT have a relation specified through InnoDB, even though message_subject
has the field message_id
.
If a relation was specified between the 2 tables with ON DELETE CASCADE
, I could just delete the record from message
and the corresponding message_subject
would automatically be removed, however this isn't the case.
I have tried:
DELETE message, message_subject
FROM message
INNER JOIN faq
WHERE message.id = message_subject.message_id
AND message.add_date < DATE_SUB(CURDATE(),INTERVAL 2 WEEK)
Which deletes messages that have a subject; messages older than 2 weeks without a subject are not deleted.
DELETE message, message_subject
FROM message
INNER JOIN message_subject
ON message.id = message_subject.message_id
WHERE message.add_date < DATE_SUB(CURDATE(),INTERVAL 2 WEEK)
does exactly the same as the first query; it doesn't delete the old messages without a subject.
Can anybody help me out here?