由于上周末小写把数据数据重复写入数据库,没办法,得去重!
最新使用的语句:
use data
set nocount on
delete DoRecordProperty from
(
select TID,SN,COUNT(0) as num,Max(id) as maxid from DoRecordProperty with (nolock) group by TID,SN having count(0)>1
) p where p.TID=DoRecordProperty.TID and p.SN=DoRecordProperty.SN and DoRecordProperty.Id<>p.maxid
set nocount off
语句执行了5分钟,还没有出结果,再执行下去,估计也没有结果。
如果单独执行
select TID,SN,COUNT(0) as num,Max(id) as maxid from DoRecordProperty with (nolock) group by TID,SN having count(0)>1
9秒钟就查询出来结果了。
于是想是否把重复的数据放在临时表里,然后再删除,效果会如何呢?
于是有了下面的语句
use data
go
set nocount on
create table #Tmp --创建临时表#Tmp
(
TID int ,
SN varchar(50),
maxid int ,
num int
);
insert into #Tmp(TID,SN,num,maxid)
select TID,SN,COUNT(0) as num,Max(id) as maxid from DoRecordProperty with (nolock) group by TID,SN having count(0)>1
delete DoRecordProperty from
#Tmp p where p.TID=DoRecordProperty.TID and p.SN=DoRecordProperty.SN and DoRecordProperty.Id<>p.maxid
Select COUNT(0) as num from #Tmp --查询临时表的数据
truncate table #Tmp --清空临时表的所有数据和约束
drop table #Tmp
set nocount off
果然,在40秒的时候,执行成功了。
以上是在服务器:CPU 2u E5-2620 ;内存 96G 硬盘是1T 企业级 7200转;数据是sql server 2008上进行操作的。