删除一张表的重复记录(案例)

删除一张表的重复记录(ID是自增唯一主键,重复记录:其他字段都是一样)
(数据量很大,性能要求很高)
表名:T
Id name age
1  louis 20
2  louis 20
3  jimmy 30
4  louis 20
做法一:
Delete from t where id not in (Select min(id) from t Group by name,age);
做法二: delete from t where id in(Select distinct a2.id from t a1,t a2 where a1.id>a2.id and a1.name=a2.name and a1.age=a2.age);
做法三:
delete from t a1 where not exists(select *
from t a2
where a1.id>a2.id and a1.name=a2.name and a1.age=a2.age
);
前提数据量>100,0000
以上三种做法,均可。但是第三种做法的性能最佳。第一种用not in没办法用到索引.第三种方式也不会用到索引
数据量 1000 100000 100,0000
方法一 0.047 3.77 72
方法二 0.286 5.77 65
第二种方式快于第一种方式。

猜你喜欢

转载自hwei-344370758.iteye.com/blog/1767463