SQL Server中删除重复数据

  最近,在项目中遇到数据库中所有的表都有大量的重复数据的问题,而且是完全的重复,即所有的字段信息都一致,包括主键都一致,需要将重复的数据删除,只保留任意一条数据。问了一些人,在网上也翻了半天,最后总结一下,共有如下几种删除重复数据的方式:

  1.对于数据量不大,效率要求不高的,比较通用的一类方法,使用游标进行删除,如下面这段脚本:

代码
     //定义两个变量

    delcare @max integer,@id integer

   
//定义一个本地游标

    declare cusCursor cursor local
for select id,count(*) from tableName group by id having count(*)>1

    
//打开游标        

    open cusCursor

    
//将当前游标读取的内容放到变量中

    fetch cusCursor into @id,@max      

    
//判断游标执行前面fetch语句后的状态,如果成功,则开始循环

    
while @@fetch_status=0      

    begin

      select @max
=@max-1

      
//设置后面语句处理的条数

      
set rowcount @max

      
//删除重复的数据,只保留一条

      delete from tableName
where id=@id  

      
//游标向下移一行,继续读取数据

      fetch cusCursor into @id,@max  

    end

    
//关闭游标

    close cusCursor

    
//删除游标

    deallocate cusCursor

    
set rowcount 0

  

   2.使用临时表的方法删除重复记录,该方法效率较高,可是有局限性,如下面这段脚本:

    

代码
    //将源表中的数据distinct以后,就只剩下唯一一条数据了,再将该条数据写入到临时表#tempTable中

    select distinct
* into #tempTable from tableName

    
//清空源表中的数据

    truncate table tableName

    
//将临时表中的数据写入到源表中

    select
* into tableName from #temTable

    
//删除临时表

    drop #tempTable 

 

  该方法存在的局限性就是,当数据库的表中存在image,text类型字段时会报错,提示该类型字段无法进行distinct操作。

  3.征对第二种方法的局限性,而且数据量也较大,对效率也有一定的要求的情况下,可以第三种方法,如下:

    

代码
    //将源表的数据结构复制到临时表#tempTable中

    select
* into #tempTable from tableName where 1<>1

    
//在临时表中创建索引,并忽略重复列数据

    Create unique index temp on #tempTable(重复列名) with IGNORE_DUP_KEY

    
//将源表中的数据拷贝到临时表中,拷贝的同时会忽略重复列的数据,也即只保留了一份数据。

    insert into #tempTable select
* from tableName

    
//清空源表
    truncate table tableName

    
//将临时表中的数据写入到源表中

    insert into tableName select
* from #tempTable      

    
//删除临时表

    drop table #tempTable

 

  以上是最近总结出来的三种删除数据库重复记录的方法,其中第三种方法在效率和通用性上都较好,在10W级数据量上都能有较好的表现。 

 

 

 

 

 

posted on 2009-12-29 22:19  RascallySnake  阅读(3698)  评论(0编辑  收藏  举报