Re: Performance issue using conditional WHERE clause



On Jan 24, 10:48 pm, Ed Murphy <emurph...@xxxxxxxxxxxx> wrote:
Jared wrote:
Consider the following two functionally identical example queries:

Query 1:

DECLARE @Name VARCHAR(32)
SET @Name = 'Bob'
SELECT * FROM Employees
WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END

Query 2:

SELECT * FROM Employees WHERE [Name] = 'Bob'

I would expect SQL Server to construct an identical QEP under the hood
for these two queries, and that they would require essentially the
same amount of time to execute.  However, Query 1 takes much longer to
run on my indexed table of ~300,000 rows.  By "longer", I mean that
Query 1 takes about two seconds, while Query 2 returns almost
instantly.

Is there a way to implement a conditional WHERE clause without
suffering this performance hit?  I want to avoid using the IF...THEN
method because I frequently require several optional parameters in the
WHERE clause.

I would at least try the following:

WHERE (@Name IS NULL OR [Name] = @Name)

as well as

WHERE NOT([Name] <> @Name)- Hide quoted text -

- Show quoted text -

Tom van Stiphout wrote:
"I don't know why you would expect the QEP to be the same. I wouldn't."

I misspoke. I didn't mean that the QEP would be identical step for
step, but rather that the performance would be pretty much the same.


Ed Murphy wrote:
I would at least try the following:
WHERE (@Name IS NULL OR [Name] = @Name)

This gives me the same performance as hard-coded values, which is to
say it returns almost instantly. I'm not sure why using CASE results
in a longer execution time, but at this point I don't really care.
=)

Thanks for your help!
.



Relevant Pages

  • Re: CREATE VIEW error
    ... exists, yes the query works in access development, asking more is time ... Is Field3 really your field name? ... The EXECUTE accepts an execute query which will be used with a ... The idea is that i need to write me complex queries, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: CREATE VIEW error
    ... exists, yes the query works in access development, asking more is time ... Is Field3 really your field name? ... The EXECUTE accepts an execute query which will be used with a ... The idea is that i need to write me complex queries, ...
    (microsoft.public.dotnet.framework.adonet)
  • SQL Injection
    ... I am currently pen-testing a web app and I am stuck in trying to execute two queries sequentially in Oracle. ... To my knowledge I can do this in SQL by separating the two queries with; however this is no happening in my case in two circumstances... ... I have a discovered an injectable sql query that is fed its data from a web form, the end query build by a cgi-script being ... This list is provided by the SecurityFocus Security Intelligence Alert ...
    (Pen-Test)
  • Re: Query from a list
    ... It works great for action queries but won't execute a "Select ... Cannot execute a select query. ... Set qry = CurrentDb.QueryDefs) ...
    (microsoft.public.access.gettingstarted)
  • Re: Strange problem?
    ... > I'm not sure if it's related with your problem but i had similar problem before, same query is working great when i execute it on my qa but when the application executes it,it timeouts. ... >>on the server looked normal (memory, services, cpu, connections, queries). ...
    (microsoft.public.sqlserver.server)