日期:2014-05-17  浏览次数:20456 次

如何快速删除重复数据。
baidu\google了一下,最快的办法是创建一个临时表,如何将数据拷入。再删除。
有没有更加快的办法。 8000万条数据左右。

------解决方案--------------------
重复的数据量大吗,需要在分组字段上建立索引

delete table where id in(
select max(id主键)as id from table group by 重复字段 having count(1)>1
)

也可以用cte+row_number()来做,效率上应该差不多吧
------解决方案--------------------
你是哪种重复?
列完全重复还是只有部分列重复

部分重复可以根据主键(id)来删除

--1
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)

--2
delete a from #T a where ID not in(select min(ID)from #T group by Name)


个人认为这两个方法不错,关键你是只为了快吗?考不考虑其他人使用这个表的情况以及发布订阅之类的。

------解决方案--------------------
WITH [CTE DUPLICATE] as
(select * from(select ROW_NUMBER() over(partition by user,xingming order by user) as a,* from table) as b
where a>1)
delete from [CTE DUPLICATE] where a>1