Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2008

Tip: Looking for answers? Try searching our database.

Performance issue using conditional WHERE clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jared - 25 Jan 2008 04:11 GMT
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
Tom van Stiphout - 25 Jan 2008 06:40 GMT
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:
>
[quoted text clipped - 23 lines]
>Thanks!
>Jared
Ed Murphy - 25 Jan 2008 06:48 GMT
> Consider the following two functionally identical example queries:
>
[quoted text clipped - 20 lines]
> 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)
Jared - 25 Jan 2008 16:51 GMT
> > Consider the following two functionally identical example queries:
>
[quoted text clipped - 30 lines]
>
> - 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.

> 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!
Erland Sommarskog - 25 Jan 2008 09:02 GMT
> Consider the following two functionally identical example queries:
>
[quoted text clipped - 15 lines]
> Query 1 takes about two seconds, while Query 2 returns almost
> instantly.

SQL Server builds the query plan for the entire batch, and thus at
compile time the value of @Name is not known. Therefore the plan must
be such that it yields a correct result in either case.

> 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 have an article on my web site that discusses a number of possible
approaches to this problem, see http://www.sommarskog.se/dyn-search.html.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Jack Vamvas - 25 Jan 2008 09:10 GMT
Try:
DECLARE @Name VARCHAR(32)
SET @Name = 'Bob'
SELECT * FROM Employees as e
INNER JOIN (SELECT id FROm Employees WHERE Name = @Name or Name IS NULL)  t2
ON  e.ID = t2.ID

Replacing ID with whatever your main Key is called

Signature

Jack Vamvas
___________________________________
Search  IT jobs from multiple sources-   http://www.ITjobfeed.com

> Consider the following two functionally identical example queries:
>
[quoted text clipped - 23 lines]
> Thanks!
> Jared
Daniel Eyer - 28 Jan 2008 14:28 GMT
SELECT * FROM Employees
WHERE [Name] = IsNull(@Name,[Name])

> Consider the following two functionally identical example queries:
>
[quoted text clipped - 23 lines]
> Thanks!
> Jared
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.