Msqlserver Tens Single-table data remove duplicate records using temporary tables

Source: Internet
Author: User

Because the last weekend lowercase data data repeatedly written to the database, there is no way to go heavy!

Recently used statements:

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 have COUNT (0) >1

) p where P.tid=dorecordproperty.tid and p.sn=dorecordproperty.sn and Dorecordproperty.id<>p.maxid

SET NOCOUNT OFF

The statement was executed for 5 minutes, and there was no result, and the execution went on, and there was no estimate.

If you perform a separate

Select Tid,sn,count (0) as Num,max (ID) as Maxid from Dorecordproperty with (NOLOCK) group by TID,SN have COUNT (0) >1

9 seconds to find out the results.

Do you want to put the duplicate data in the temporary table, and then delete, the effect will be?

So there's the following statement

Use data
Go

SET NOCOUNT ON

CREATE TABLE #Tmp--Creating a temporary table #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 have 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--query data for temporary tables
TRUNCATE TABLE #Tmp-Clears all data and constraints for the staging table
drop table #Tmp
SET NOCOUNT OFF

Sure enough, in 40 seconds, the execution was successful.

The above is on the server: CPU 2u e5-2620; memory 96G HDD is 1T enterprise level 7200 RPM; Data is operational on SQL Server 2008.

Msqlserver Tens Single-table data remove duplicate records using temporary tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.