optimization - SQL SERVER 2012, 10 Million Rows, Match Multiple Fields In Same Table To Get Results -


have 10 million records in temporary table called #temp. columns are

primarykey, field1, field2, fields3, field4, field5, field6, datefield 

where primarykey in numeric while other fields varchar except datefield.

now want duplicate records table in format.

isduplicate, primarykey1, primarykey2, datefield1, datefield2 

currently, following query running it's taking very long time execute.

   data (select d.field1 key1, d.* #temp d  d.field1 not null union select d.field2 key1, d.* #temp d  d.field2 not null union select d.field3 key1, d.* #temp d  d.field3 not null union select d.field4 key1, d.* #temp d  d.field4 not null union select d.field5 key1, d.* #temp d  d.field5 not null ) select distinct isduplicate, pk1, pk2, date1, date2 ( select case when case when t1.field1 = t2.field1 , t1.field1 not null , t1.field1 != ' ' 1 else 0 end+case when t1.field2 = t2.field2 , t1.field2 not null , t1.field2 != '' 1 else 0 end+case when t1.field3 = t2.field3 , t1.field3 not null , t1.field3 != '' 1 else 0 end+ case when t1.field4 = t2.field4 , t1.field4 not null 1 else 0 end+ case when t1.field5 = t2.field5 , t1.field5 not null , t1.field5 != '' 1 else 0 end+ case when t1.field6 = t2.field6 , t1.field6 not null , t1.field6 != '' 1 else 0 end >= 4 1 else 0 end isduplicate, case when t1.field1 = t2.field1 , t1.field1 not null , t1.field1 != ' ' 1 else 0 end+ case when t1.field3 = t2.field3 , t1.field3 not null , t1.field3 != '' 1 else 0 end+ case when t1.field4 = t2.field4 , t1.field4 not null 1 else 0 end+ case when t1.field5 = t2.field5 , t1.field5 not null , t1.field5 != '' 1 else 0 end+ case when t1.field6 = t2.field6 , t1.field6 not null , t1.field6 != '' 1 else 0 end ispotential,  t1.pk pk1, t2.pk pk2, t1.date date1, t2.date date2 data t1 inner join data t2 on t1.key1 = t2.key1 ) r r.isduplicate = 1 or r.ispotential >= 3 

looking alternate way desired result in optimised way.

thanks.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -