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

Avoid conditional logic?

 
   Database Help (Home) -> Programming RSS
Next:  Strange situation - DB2  
Author Message
Marty McDonald

External


Since: Jul 23, 2010
Posts: 1



(Msg. 1) Posted: Fri Jul 23, 2010 8:09 am
Post subject: Avoid conditional logic?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I've heard that SQL Server can create a better execution plan if the
query has unconditional logic as opposed to conditional logic. So
where I might want to do this (conditional logic)...
IF @Test = 1
SELECT This, That From Table WHERE This = 'aaa'
ELSE
SELECT This, That From Table WHERE This = 'bbb'

....I will do this instead (unconditional logic)...
SELECT This, That From Table WHERE This = 'aaa' AND @Test = 1
UNION ALL
SELECT This, That From Table WHERE This = 'bbb' AND @Test = 2

Can SQL Server really handle the 2nd style better?
Thanks
Marty

 >> Stay informed about: Avoid conditional logic? 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 2) Posted: Fri Jul 23, 2010 8:51 am
Post subject: Re: Avoid conditional logic? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Using IF to conditionally execute queries will be more efficient. Take
a look at the query plans in both cases and that will show you the
difference.

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: Avoid conditional logic? 
Back to top
Login to vote
Gert-Jan Strik

External


Since: Feb 19, 2010
Posts: 6



(Msg. 3) Posted: Fri Jul 23, 2010 1:25 pm
Post subject: Re: Avoid conditional logic? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Marty McDonald wrote:
>
> I've heard that SQL Server can create a better execution plan if the
> query has unconditional logic as opposed to conditional logic. So
> where I might want to do this (conditional logic)...
> IF @Test = 1
> SELECT This, That From Table WHERE This = 'aaa'
> ELSE
> SELECT This, That From Table WHERE This = 'bbb'
>
> ...I will do this instead (unconditional logic)...
> SELECT This, That From Table WHERE This = 'aaa' AND @Test = 1
> UNION ALL
> SELECT This, That From Table WHERE This = 'bbb' AND @Test = 2
>
> Can SQL Server really handle the 2nd style better?
> Thanks
> Marty

If there is any performance difference, then the "conditional logic"
version is faster.

If you are unlucky, and the engine "forgets" to shortcut the execution,
the UNION ALL query does an unnecessary table access.
--
Gert-Jan
 >> Stay informed about: Avoid conditional logic? 
Back to top
Login to vote
Tony Rogerson

External


Since: Jan 10, 2008
Posts: 213



(Msg. 4) Posted: Fri Jul 23, 2010 1:25 pm
Post subject: Re: Avoid conditional logic? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If you are on SQL 2008 or SQL 2008 R2 and on the latest SP and CU then you
can use OPTION( RECOMPILE ) to good effect because of embedded parameter
optimisation.

However, why don't you just do this instead...

declare @this_lookup char(3);

set @this_lookup = case when @test = 1 then 'aaa' when @test = 2 then 'bbb'
else null end

SELECT This, That From Table
WHERE This = @this_lookup

Tony.

"Marty McDonald" wrote in message

> I've heard that SQL Server can create a better execution plan if the
> query has unconditional logic as opposed to conditional logic. So
> where I might want to do this (conditional logic)...
> IF @Test = 1
> SELECT This, That From Table WHERE This = 'aaa'
> ELSE
> SELECT This, That From Table WHERE This = 'bbb'
>
> ...I will do this instead (unconditional logic)...
> SELECT This, That From Table WHERE This = 'aaa' AND @Test = 1
> UNION ALL
> SELECT This, That From Table WHERE This = 'bbb' AND @Test = 2
>
> Can SQL Server really handle the 2nd style better?
> Thanks
> Marty
 >> Stay informed about: Avoid conditional logic? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
And/Or Logic - I create a report based on values passed from a front end. The user selects the statuses they want to see from checkboxes. The user can select a few different statuses.They can either select All, Pending, InReview, Cleared. If they select all I display....

help on if logic - -snip- if (@TotalInTodayFromSameIp<10) and (@PublisherLastEranedDifferance > 10) and (NOT @PublisherLastInAgent=@browser) and (NOT @PublisherLastInIp=@ip) and (@selectedid is not null) BEGIN -snip above IF runs query below BEGIN when all matches..

How to avoid Deadlocks - I am facing the following problem with deadlocks: I have written a set of stored procedures which invoices orders in a database. First some general explanation: There exists a table for orders and a table for order positions referencing the order..

Need help with query syntax/logic - Table GPS_Vehicle columns: siteID vehicleID firstName lastName datetimestamp lat long I would like to select only records with datetimestamp = max(datetimestamp) for each vehicleID I used the following query and it worked well. Only that I would like...

Logic in SP query - Hi, I am trying to determine if I should be using a cursor or if I can put my logic right into the select statement. I'm selecting my results and putting them into a temp table. In the value1 column, the following data appears: "Name: John Doe&quo...
   Database Help (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 ]