由于行数过多,删除连接未完成

Delete with join not completing due to high number of rows

我有一个 table 大约有 55 万行。我正在尝试执行此查询:

DELETE t1 FROM categories t1
INNER JOIN categories t2
WHERE t1.id < t2.id
  AND t1.name = t2.name
  AND t1.book_id = t2.book_id

不幸的是 shell 冻结了,我可以通过计算另一个 shell 中的行数来判断没有发生任何事情。

有什么方法可以缓冲这个查询,或者以其他方式解决这个问题吗?

感谢任何帮助。

如果需要删除大量行,通常将要保留的行移动到另一个 table,截断原来的 table,然后再插入回更有效它:

-- move the rows we want to keep
create table t1_tmp as 
select name, book_id, min(id) id from t1 group by name, book_id;

-- empty the table - back it up first! 
truncate table t1;

-- insert back into the table
insert into t1 (id, name, book_id) 
select id, name, book_id from t1_tmp;

-- drop the temporary table
drop table t1_tmp;

这个索引应该有帮助:

INDEX(name, book_id, id)