Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Using Case Conditions in WHERE clause

 
   Database Forums (Home) -> Programming RSS
Next:  Named Set returning hierarchy (SQL 2005)  
Author Message
HP

External


Since: Mar 03, 2005
Posts: 4



(Msg. 1) Posted: Thu Dec 29, 2005 1:06 pm
Post subject: Using Case Conditions in WHERE clause
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Can i use case conditions in where clause? does it affect the performance?

eg.

select fldnames from table
where type =
case when @ctype = '1' then 'quote'
else 'order'
end

Thanks

 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
Anith Sen

External


Since: Feb 17, 2004
Posts: 280



(Msg. 2) Posted: Thu Dec 29, 2005 3:14 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> Can i use case conditions in where clause?

Yes, you can. What you have done is very reasonable. There are a variety of
options, some of which can be found at: www.sommarskog.se/dyn-search.html

>> does it affect the performance?

It depends on a different factors and in many cases it may. You may want to
test the various options out and see.

--
Anith

 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
HP

External


Since: Mar 03, 2005
Posts: 4



(Msg. 3) Posted: Thu Dec 29, 2005 3:14 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Is there an alternative for case statements?

"Anith Sen" wrote:

> >> Can i use case conditions in where clause?
>
> Yes, you can. What you have done is very reasonable. There are a variety of
> options, some of which can be found at: www.sommarskog.se/dyn-search.html
>
> >> does it affect the performance?
>
> It depends on a different factors and in many cases it may. You may want to
> test the various options out and see.
>
> --
> Anith
>
>
>
 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
Alexander Kuznetsov

External


Since: Sep 09, 2005
Posts: 65



(Msg. 4) Posted: Thu Dec 29, 2005 3:22 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> But in
>
> SELECT ...
> FROM ...
> WHERE @value = CASE @type
> WHEN 1 THEN indexedcol1
> ELSE indexedcol2
> END
>
>It could still use a plan where it tries both indexes (and will only
>access one of them at run time.)

but let's make the life easier for the optimizer:

if(@type = 1)
SELECT ...
FROM ...
WHERE @value = indexedcol1
else
SELECT ...
FROM ...
WHERE @value = indexedcol2

the chances you'll get a better plan could be higher. Makes sense?
 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
Trey Walpole

External


Since: Nov 10, 2005
Posts: 20



(Msg. 5) Posted: Thu Dec 29, 2005 3:31 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

did you try it?
did you look at the execution plan? [estimated or actual]

iow, you could probably answer this question better yourself than
someone on a ng.

performance depends on your indexing, the number of rows in the table,
selectivity of the criteria used, etc.
throwing a case into the mix adds to this list the complexity of the
when expressions [are they scalar, like the example, or are they
exists(), etc.]

the case may not have anything to do with why it's slow, e.g. in the
given example, if you have millions of rows and only 2 values in this
column, even ... where type='order' ... will probably be slow.




HP wrote:
> Can i use case conditions in where clause? does it affect the performance?
>
> eg.
>
> select fldnames from table
> where type =
> case when @ctype = '1' then 'quote'
> else 'order'
> end
>
> Thanks
>
>
>
>
 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
HP

External


Since: Mar 03, 2005
Posts: 4



(Msg. 6) Posted: Thu Dec 29, 2005 3:31 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks

"Trey Walpole" wrote:

> did you try it?
> did you look at the execution plan? [estimated or actual]
>
> iow, you could probably answer this question better yourself than
> someone on a ng.
>
> performance depends on your indexing, the number of rows in the table,
> selectivity of the criteria used, etc.
> throwing a case into the mix adds to this list the complexity of the
> when expressions [are they scalar, like the example, or are they
> exists(), etc.]
>
> the case may not have anything to do with why it's slow, e.g. in the
> given example, if you have millions of rows and only 2 values in this
> column, even ... where type='order' ... will probably be slow.
>
>
>
>
> HP wrote:
> > Can i use case conditions in where clause? does it affect the performance?
> >
> > eg.
> >
> > select fldnames from table
> > where type =
> > case when @ctype = '1' then 'quote'
> > else 'order'
> > end
> >
> > Thanks
> >
> >
> >
> >
>
 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jun 15, 2005
Posts: 632



(Msg. 7) Posted: Thu Dec 29, 2005 4:39 pm
Post subject: Re: Using Case Conditions in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Is there an alternative for case statements?

Sure, one trick I learned here and have adopted is for things like positive
INTegers, you can help the optimizer choose a seek instead of a scan by
doing something like this:

CREATE PROCEDURE dbo.foo
@value INT = NULL
AS
BEGIN
SET NOCOUNT ON;

SELECT col1,col2,col3 FROM table
WHERE value >= COALESCE(@value, 0)
AND value <= COALESCE(@value, 2147483647);
END
GO

This way, if you have an index on the column 'value', you aren't hurt as
much by optional parameters, as opposed to this case, which is much more
likely to cause a scan and slow down your query:

WHERE value = COALESCE(@value, value);

...which is essentially shorthand for:

WHERE value = CASE WHEN @value IS NULL THEN value ELSE @value END;

Of course, when you have multiple ways of getting something done, you should
always test performance *in your environment* and *against your data.* It
is very difficult for any of us to give you a straight answer about which
way is faster unless we have a very comprehensive understanding of your
hardware, data model and usage.

I am not even clear exactly how you intend to use case in all scenarios, but
I will say that there is often an alternative, and I will state again that
there is no clear-cut answer that option a is better than option b in all
cases. This is something you learn over time, with experience, and by
testing every time you change anything.
 >> Stay informed about: Using Case Conditions in WHERE clause 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
T-SQL does not support breakpoint Conditions - When attempting to select the breakpoint condition from within the VS05 RTM IDE I get the following error message: "T-SQL does not support breakpoint Conditions" What's up with that? Also, why the heck would you not be able to debug TSQL...

Avoiding race conditions - Hi, I'm having problems with race conditions in my application. It uses the nested set model to store trees in the database. Unfortunately, to insert an item into a tree, there are 1 SELECT and 2 Update and 1 INSERT statements. The last 3 statements..

CASE statement with an IN - hi there, I am trying to do this, it would simply my sql so much, but the syntax won't parse. DECLARE @Tmp varchar --@Tmp will either be cow or pig after something done here, lets say pig for simplification.. SET @Tmp = 'pig' SELECT * From Blah WHER...

Case in Join - Can you put a Case statement in a Join? My problem is that I have 2 possible fields I want to join to the same table. If one is null, use the other. For example: Create Table Position ( UserID1 int, UserID2 int ) Create Table Logon ( ..

CASE problem - Hi all, I'm trying to return a list of country id's based on a condition, but I cant seem to get it to work, It seems to be because of the 'IN' join but I cant work out why? Are 'IN's not allowed for Case statements? Regards Dan DECLARE @CT_ID in...
   Database Forums (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]