I’m trying to delete orphan posts in my database and I have created this query:
DELETE post.*
FROM foro_post AS post
LEFT JOIN foro_thread AS thread USING(threadid)
WHERE thread.threadid IS NULL
The problem is that I want to limit because my table has over 7,000,000 records.
As I can’t use LIMIT with the query, I tried this and actually worked, but I’m not sure if is an efficient solution or if it could be done better.
DELETE post.*
FROM foro_post AS post
LEFT JOIN foro_thread AS thread USING(threadid)
WHERE thread.threadid IS NULL
AND post.postid < 500
// Where < 500 should be increasing as I delete records
How can I do this more efficiently?
You can’t use
LIMITdirectly within DELETE when you’re referencing multiple tables at the same time, but you can get around that by encasing what you want to delete within a subselect: