if you have optional parameters in a stored procedure, this is a better way to do the comparison. by optional, i mean they will either be set to null, or will be null because they are not passed in or are otherwise not set. of course, realize indexing can affect performance as well as many other factors. you should always test. generally, this will be faster time wise. occasionally, i have found coalesce to incur more cpu time, but that is normally repaid by a faster turn around time as it seems to eliminate more rows quickly in the plan.
this is a general rule, not a golden one, so i'll re-iterate again that you should test all optimizations.
i've included the commands so you can look at the execution plans, io, and time analysis. for simplistic seperation, i will term the generally slower practice as 'bad' and the faster one as 'good'. in my example below @v and @c are varchar(50) and @a is a char(1). feel free to change everything, the case for coalesce generally holds true across the board.
bad
DBCC DROPCLEANBUFFERS
dbcc freeproccache
set statistics io on
set statistics time on
set statistics profile on
select
id
, addressline1
, city
, active
from
person
where
@v is null or addressline1 like @v + '%'
and @c is null or city like @c + '%'
and @a is null or active = @a
set statistics time off
set statistics io off
set statistics profile off
good
DBCC DROPCLEANBUFFERS
dbcc freeproccache
set statistics profile on
set statistics io on
set statistics time on
select
id
, addressline1
, city
, active
from
person
where
addressline1 like coalesce(@v,addressline1) + '%'
and city like coalesce(@c,city) + '%'
and active = coalesce(@a,active)
set statistics time off
set statistics io off
set statistics profile off
i also found several other good articles about this. some of differing opinion. most way better than this post. =P
http://blogs.x2line.com/al/archive/2004/03/01/189.aspxhttp://sqljunkies.com/WebLog/amachanic/archive/2004/11/30/5311.aspxhttp://weblogs.sqlteam.com/mladenp/articles/2937.aspxmy post is really only about examining the case for optional parameters or null values. some of these are about isnull vs coalesce. i'm a fan of coalesce personally.
posted @ Saturday, March 17, 2007 5:02 PM