I have a query that has a where clause built using a number of local variables, This however is painfully slow. Below is a rough example as I don't have access to the query currently:
declare @a varchar(50), @b varchar(50), @c varchar(50)
set @a = '%'
set @b = 'foo'
set @c = '%bar'
My where clause s something like
where a = @a and b = @b and c =@c
This take approx 1 minute to run. However, if I directly reference the values in the where clause such as:
where a = '%' and b = 'foo' and '%bar'
It takes approx 5 seconds.
So my question is, is there a better way to contruct my where clause? One important thing to note. There are about 10 local variables used in the where clause, but most are set to a default of %
Thanks in advance