大型SQL数据库删除重复的行

时间:2013-10-01 15:40:33

标签: mysql sql sql-server-2008 duplicates sql-delete

包含60 000 000条记录的大型SQL 2008表,我遇到重复行的问题。

这个命令让我复制了我的副本 http://support.microsoft.com/kb/139444

SELECT     id, sa_trvalue,  COUNT(*) AS tot  
FROM         msanal   
GROUP BY id, sa_trvalue  
HAVING      (COUNT(*) > 1)  

但是当我按照步骤(INTO和DISTINCT)进行操作时,我没有足够的内存来完成操作。

2 个答案:

答案 0 :(得分:1)

您可以尝试这种可能需要更少内存的方法:

WITH CTE AS
(
    SELECT  id, sa_trvalue, 
            rn = ROW_NUMBER() OVER (PARTITION BY id, sa_trvalue ORDER BY id ASC)
    FROM    msanal   
)
DELETE FROM CTE WHERE rn > 1

公用表表达式的优点还在于您可以轻松修改它以查看要删除的内容。因此,您只需将DELETE更改为SELECT *

答案 1 :(得分:0)

delete msanal from msanal m1
where exists
(select null from msanal m2
where m2.sa_trvalue = m1.sa_trvalue and m2.id <> m1.id)