Re: Performance issue using conditional WHERE clause



On Thu, 24 Jan 2008 20:11:21 -0800 (PST), Jared
<blacktoe.the.crippler@xxxxxxxxx> wrote:

I don't know why you would expect the QEP to be the same. I wouldn't.

It would be interesting to see if COALESCE would cause the same
performance bottleneck. Can you try that on your system?
-Tom.


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.

Thanks!
Jared
.



Relevant Pages

  • Re: [Info-Ingres] To order by, or NOT to order by
    ... I'm looking for a way to make the optimizer come up with the ... [Info-Ingres] To order by, ... QEPs with a sample query ... Query and QEP, ...
    (comp.databases.ingres)
  • Re: [Info-Ingres] To order by, or NOT to order by
    ... [Info-Ingres] To order by, ... but then dropped the -zs flag altogether when there were no changes in the QEP. ... the best QEP but I guess the optimizer in 2.6/604 is a bit different. ... QEPs with a sample query ...
    (comp.databases.ingres)
  • Re: [Info-Ingres] To order by, or NOT to order by
    ... but then dropped the -zs flag altogether when there were no changes in the QEP. ... the best QEP but I guess the optimizer in 2.6/604 is a bit different. ... [Info-Ingres] To order by, ... QEPs with a sample query ...
    (comp.databases.ingres)
  • Re: Real time decision making based on QEP results
    ... and Fix Long running Queries) which requires monitoring the system ... whether or not to execute a query based on ... It would be nice if the QEP could be presented in a form that is more ... prompt the user to continue with the report generation or not. ...
    (comp.databases.ingres)
  • Re: Real time decision making based on QEP results
    ... and Fix Long running Queries) which requires monitoring the system ... What I am trying to accomplish is to use the qep results as means of ... whether or not to execute a query based on ... prompt the user to continue with the report generation or not. ...
    (comp.databases.ingres)