我正在尝试删除数据库中的孤立帖子,并创建了此查询:
DELETE post.*
FROM foro_post AS post
LEFT JOIN foro_thread AS thread USING(threadid)
WHERE thread.threadid IS NULL
问题在于我想限制,因为我的表有超过7,000,000条记录。
由于我不能在查询中使用LIMIT,我尝试了这个并且确实有效,但我不确定它是否是一种有效的解决方案,或者是否可以做得更好。
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
如何更有效地完成这项工作?
答案 0 :(得分:15)
当您同时引用多个表时,不能在DELETE内直接使用LIMIT
,但是您可以通过在子选择中包含要删除的内容来解决这个问题:< / p>
DELETE po
FROM foro_post po
JOIN (
SELECT p.postid
FROM foro_post p
LEFT JOIN foro_thread t ON p.threadid = t.threadid
WHERE t.threadid IS NULL
ORDER BY p.postid
LIMIT 50
) pp ON po.postid = pp.postid