15

我正在尝试删除数据库中的孤立帖子,并且创建了以下查询:

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

我怎样才能更有效地做到这一点?

4

1 回答 1

26

当您同时引用多个表时,您不能LIMIT直接在DELETE中使用,但您可以通过将要删除的内容包含在子选择中来解决这个问题:

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
于 2012-08-30T17:51:20.413 回答