Unix Technical Forum

Performance issue using conditional WHERE clause

This is a discussion on Performance issue using conditional WHERE clause within the SQL Server forums, part of the Microsoft SQL Server category; --> Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:47 PM
Jared
 
Posts: n/a
Default Performance issue using conditional WHERE clause

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:47 PM
Tom van Stiphout
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

On Thu, 24 Jan 2008 20:11:21 -0800 (PST), Jared
<blacktoe.the.crippler@gmail.com> 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:47 PM
Ed Murphy
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:47 PM
Jack Vamvas
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

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


--

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




"Jared" <blacktoe.the.crippler@gmail.com> wrote in message
news:d5382a71-4306-4603-809f-9082269c9a85@s8g2000prg.googlegroups.com...
> 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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 03:47 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

Jared (blacktoe.the.crippler@gmail.com) writes:
> 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.


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.


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 03:47 PM
Jared
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

On Jan 24, 10:48*pm, Ed Murphy <emurph...@socal.rr.com> 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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 03:48 PM
Daniel Eyer
 
Posts: n/a
Default Re: Performance issue using conditional WHERE clause

SELECT * FROM Employees
WHERE [Name] = IsNull(@Name,[Name])

"Jared" <blacktoe.the.crippler@gmail.com> a écrit dans le message de news:
d5382a71-4306-4603-809f-9082269c9a85...oglegroups.com...
> 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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com