MySQL remove duplicates from big database quick

I believe this will do it, using on duplicate key + ifnull():

create table tmp like yourtable;

alter table tmp add unique (text1, text2);

insert into tmp select * from yourtable 
    on duplicate key update text3=ifnull(text3, values(text3));

rename table yourtable to deleteme, tmp to yourtable;

drop table deleteme;

Should be much faster than anything that requires group by or distinct or a subquery, or even order by. This doesn’t even require a filesort, which is going to kill performance on a large temporary table. Will still require a full scan over the original table, but there’s no avoiding that.

Leave a Comment