大数据量的数据中如何删除重复的记录
现在一表A(id,name)里面有几十万的数据量。有些数据是重复的,就是字段name的数据相同。如何在高效率的前提下删除重复数据,重复的保留一条记录。除了建临时表还有什么其它的办法没?
假设ID唯一
在NAME、ID上建立 索引
select * from tt a where not exists(select 1 from tt where a.name=name and a.id>id)
临时表的效率应该是比较高的。create table x as select id,name from yourTable group by name
虽然你可以
delete from yourTable a where exists (select 1 from yourTable where id>a.id and name=a.name)
建立一张新表,将旧表数据导入新表(通过sql完成)
Insert into Table2(name,id) select distinct name,id from Table1
或者通过 SELECT INTO FROM语句 语句!
-
SQL code
DELETE FROM 表A a WHERE EXISTS(SELECT 1 FROM 表A WHERE id>a.id and a.name=name);
就用临时表好了
临时表的效率是比现在的查询效率要高的多。
探讨
SQL code
DELETE FROM 表A a
WHERE EXISTS(SELECT 1 FROM 表A WHERE id>a.id and a.name=name);