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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Coalesce dramatically increasing execution speed?

Author  Topic 

carlb007
Starting Member

2 Posts

Posted - 2009-09-09 : 11:54:12
Hi all - first post here; i usually like to figure out my issues with trial and error but this one totally has me beat!

Basically ive got some SQL that is used for paging through a set of 2000 properties that compares against a whole range of features etc. If you look at the code below you will see that theres about 20lines commented out. When i run the code with these lines disabled the execution is instant. HOWEVER, when i enable those lines (1 of them or all of them it makes no difference at all) the execution time jumps to over 40seconds!!
I have absolutely no idea why its doing this, or why this should happen? Ive used the same code for paging many times in the past and whilst it may not be the most effecient it has never taken so long to run.

Please take a look at my code - any help would be hugely welcomed!

Thanks in advance,
Carl.


Select top (@param5) [Name],Address,PostCode,p.AccommodationCode,AccommodationDescription,Occupancy,MinPrice,MaxPrice,Facilities,Features,AdultSleeps,ChildrenSleeps,InfantSleeps,Longitude,Latitude--,(Select top 1 url from propertyImages as pri where pri.accommodationCode=p.AccommodationCode) as url
from properties as p
Where
RegionCode=coalesce(@param1,RegionCode,Null)
and SubRegionCode=coalesce(@param2,SubRegionCode,Null)
and SubSubRegionCode=coalesce(@param3,SubSubRegionCode)
and AdultSleeps>=@Crit1 and ChildrenSleeps>=@Crit2 and InfantSleeps>=@Crit3
--and (
-- Feature1=ISNULL(@Feature1,Feature1)
-- and Feature2=Coalesce(@Feature2,Feature2)
-- and Feature3=Coalesce(@Feature3,Feature3)
-- and Feature4=Coalesce(@Feature4,Feature4)
-- and Feature5=Coalesce(@Feature5,Feature5)
-- and Feature6=Coalesce(@Feature6,Feature6)
-- and Feature7=Coalesce(@Feature7,Feature7)
-- and Feature8=Coalesce(@Feature8,Feature8)
-- and Feature9=Coalesce(@Feature9,Feature9)
-- and Feature10=Coalesce(@Feature10,Feature10)
-- and Feature11=Coalesce(@Feature11,Feature11)
-- and Feature12=Coalesce(@Feature12,Feature12)
-- and Feature13=Coalesce(@Feature13,Feature13)
-- and Feature14=Coalesce(@Feature14,Feature14)
-- and Feature15=Coalesce(@Feature15,Feature15)
-- and Feature16=Coalesce(@Feature16,Feature16)
-- and Feature17=Coalesce(@Feature17,Feature17)
-- and Feature18=Coalesce(@Feature18,Feature18)
-- and Feature19=Coalesce(@Feature19,Feature19)
-- and Feature20=Coalesce(@Feature20,Feature20)
--)
and [id] not in(
select top (@param5*(@param4-1)) id
from properties as p
Where
RegionCode=coalesce(@param1,RegionCode,Null)
and SubRegionCode=coalesce(@param2,SubRegionCode,Null)
and SubSubRegionCode=coalesce(@param3,SubSubRegionCode)
and AdultSleeps>=@Crit1 and ChildrenSleeps>=@Crit2 and InfantSleeps>=@Crit3
and (
Feature1=Coalesce(@Feature1,Feature1)
and Feature2=Coalesce(@Feature2,Feature2)
and Feature3=Coalesce(@Feature3,Feature3)
and Feature4=Coalesce(@Feature4,Feature4)
and Feature5=Coalesce(@Feature5,Feature5)
and Feature6=Coalesce(@Feature6,Feature6)
and Feature7=Coalesce(@Feature7,Feature7)
and Feature8=Coalesce(@Feature8,Feature8)
and Feature9=Coalesce(@Feature9,Feature9)
and Feature10=Coalesce(@Feature10,Feature10)
and Feature11=Coalesce(@Feature11,Feature11)
and Feature12=Coalesce(@Feature12,Feature12)
and Feature13=Coalesce(@Feature13,Feature13)
and Feature14=Coalesce(@Feature14,Feature14)
and Feature15=Coalesce(@Feature15,Feature15)
and Feature16=Coalesce(@Feature16,Feature16)
and Feature17=Coalesce(@Feature17,Feature17)
and Feature18=Coalesce(@Feature18,Feature18)
and Feature19=Coalesce(@Feature19,Feature19)
and Feature20=Coalesce(@Feature20,Feature20)
)
order by [name] asc
)
order by [name] asc
return

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-09-09 : 15:14:44
Yeah, catch-all-queries are very prone to executing poorly.

There are a couple of options that I am aware of: use Dynamic SQL or store the results in a temp table and then filter on the Features.

But, it's hard to say what is going on since I don't know anything about the size of the data set, the selectivity of the filters, the indexes on the tables nor the execution plan than was generated.

Here is an article by Gail that talks a bit about this issue:
[url]http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url]
Go to Top of Page

asgast
Posting Yak Master

149 Posts

Posted - 2009-09-10 : 04:25:39
you are trying to do the same search two times :) once in a where and in sub select
If you have an ID columns in our property table the following solution might help
select the ids you need into a temp table, create an index on our temp table
later just inner join your temp table to properties on ID field instead of searching using a condition in a where
you will get better response time
Go to Top of Page

carlb007
Starting Member

2 Posts

Posted - 2009-09-10 : 04:35:36
Thanks for your responses guys - gonna do some reading on the subject.

As it happens ive managed to 'fix' the issue by swapping those coalesces to:
(Feature1=@Feature1 or @Feature1 IS NULL)
and (Feature2=@Feature2 or @Feature2 IS NULL)
and (Feature3=@Feature3 or @Feature3 IS NULL)
etc etc...

Its still probably no more effecient but at least it runs <1second now - Im still none the wiser as to why those outer coalesces or ISNulls made the thing so damn slow though...40seconds to 0 almost seems like a bug to me - especially if you run the statment without the sub select it works just fine.

Cheers again,
Carl.
Go to Top of Page

asgast
Posting Yak Master

149 Posts

Posted - 2009-09-10 : 04:53:59
:(
looking at your code, for a second I thought, that maybe using col = ISNULL(@variable, col) would work faster then using (col = @variable OR @variable is null)

I still think that you need to use temp table instead of running the same complex query two times

Go to Top of Page
   

- Advertisement -