(I found the same question exists, but was not happy with the detailed specification, so came here for help, forgive me for my ignorance)
DELETE FROM supportrequestresponse # ~3 million records
WHERE SupportRequestID NOT IN (
SELECT SR.SupportRequestID
FROM supportrequest AS SR # ~1 million records
)
Or
DELETE SRR
FROM supportrequestresponse AS SRR # ~3 million records
LEFT JOIN supportrequest AS SR
ON SR.SupportRequestID = SRR.SupportRequestID # ~1 million records
WHERE SR.SupportRequestID IS NULL
Specifics
- Database: MySQL
- SR.SupportRequestID is INTEGER PRIMARY KEY
- SRR.SupportRequestID is INTEGER INDEX
- SR.SupportRequestID & SRR.SupportRequestID are not in FOREIGN KEY relation
- Both tables contain
TEXT
columns for subject and message - Both tables are InnoDB
Motive: I am planning to use this with a periodic clean up job, likely to be once an hour or every two hours. It is very important to avoid lengthy operation in order to avoid table locks as this is a very busy database and am already over quota with deadlocks!
EXPLAIN query 1
1 PRIMARY supportrequestresponse ALL 410 Using where
2 DEPENDENT SUBQUERY SR unique_subquery PRIMARY PRIMARY 4 func 1 Using index
EXPLAIN query 2
1 SIMPLE SRR ALL 410
1 SIMPLE SR eq_ref PRIMARY PRIMARY 4 SRR.SupportRequestID 1 Using where; Using index; Not exists
RUN #2
EXPLAIN query 1
1 PRIMARY supportrequestresponse ALL 157209473 Using where
2 DEPENDENT SUBQUERY SR unique_subquery PRIMARY PRIMARY 4 func 1 Using index; Using where; Full scan on NULL key
EXPLAIN query 2
1 SIMPLE SRR ALL 157209476
1 SIMPLE SR eq_ref PRIMARY PRIMARY 4 SRR.SupportRequestID 1 Using where; Using index; Not exists