0

我正在尝试运行下面的 sql 查询,但经过长时间的等待后出现 505 错误。wp_posts 表包含超过 50,000 行...目的是删除大约 50,000 行的重复帖子

DELETE bad_rows.*
FROM wp_posts AS bad_rows
INNER JOIN 
(
     SELECT post_title, MIN(id) as min_id
     FROM wp_posts
     GROUP BY post_title
     HAVING count(*) > 1
) AS good_rows 
ON good_rows.post_title = bad_rows.post_title
AND good_rows.min_id <> bad_rows.id;

关于如何优化和运行的任何想法?

4

1 回答 1

4

不要认为您需要使用子查询。这将选择所有具有相同标题的帖子对。

SELECT a.id, b.id
FROM wp_posts a
LEFT JOIN wp_posts b 
ON a.post_title = b.post_title
WHERE a.id > b.id

如果您想保留最旧的帖子,请删除 ID 为 a.id 的所有帖子。如果您想保留最新的帖子,请删除所有 id 为 b.id 的帖子。


因此,要保留最旧的帖子:

DELETE FROM wp_posts a
LEFT JOIN wp_posts b 
ON a.post_title = b.post_title
WHERE a.id > b.id

保持最新的帖子:

DELETE FROM wp_posts a
LEFT JOIN wp_posts b 
ON a.post_title = b.post_title
WHERE a.id < b.id
于 2012-06-09T02:13:56.460 回答