Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help tuning a query.

Author  Topic 

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-05-03 : 15:09:00
so just a performance question here... for some reason, this query runs LONG if i leave it the way it is (long as in i've let it run for a good 30 minutes w/ no results). but if i take the left join on the DNC and move it to the outside query (before the where recid=1) it's done in 3 seconds... any insight on why it might be working like that?


select STATE,COUNT(state)
from (select T.*,z.COUNTY,ROW_NUMBER() over (partition by t.phone order by t.date_appen desc) as recid
from TestDB.tru.truecredit t
left join zipinfo.dbo.GeoLookup z on z.zip = t.zip
left join Residential.dbo.dnc d on d.tl1 = t.PHONE
left join Korey.dbo.supp s
on LEFT(s.lname,5) = LEFT(t.lname,5)
and LEFT(s.address,10) = LEFT(t.address,10)
and LEFT(s.zip,5) = LEFT(t.zip,5)
where 1=1
and t.date_appen between getdate()-180 and getdate()-30
and d.tl1 is null
and t.phone>''
and s.lname is null
and s.address is null
and s.zip is null
and t.STATE in ('AL','AK','AZ','CA','CT','DC','FL','IL','IN','LA','MD',
'MA','MI','MT','NE','NH','NM','NY','ND','OK','OR','PA',
'SD','TX')
and t.rev16>=20000
) as f
where recid=1
group by STATE order by state

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-05-03 : 15:42:49
Do you have any indexes on table?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-05-03 : 15:47:34
what are the table sizes?

It might be that the left join is generating lots of rows that then need to have the LEFT string fucntions applied or the ROW_NUMBER needs to group and sort. Hard to say from just a query.
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-05-03 : 17:03:29
the supp table has one index for lname/address/zip (about 4M)
zip table is index on the zipcode (about 80k)
the dnc table has a clustered pk on the tl1 column (and thats all that's in the table) (about 180M records)
and the truecredit table is about 8M rows, and has a crap-load of indexing...

i just found it odd that if i use the exact same code, but put the one part that was slowing it down on the outside query, instead of the inside, that it would be so much faster.
Go to Top of Page
   

- Advertisement -