由于行数过多,删除并没有完成联接

问题描述

我有一个约有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中的行来判断什么都没有发生。

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

感谢您的帮助。

解决方法

如果您需要删除大量行,通常将要保留的行移到另一个表,截断原始表,然后再插入其中,这样会更有效:

-- 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,book_id from t1_tmp;

-- drop the temporary table
drop table t1_tmp;
,

该索引应有帮助:

INDEX(name,id)