Unix Technical Forum

a and (b or c) better than (a and b) or (a and c) ?

This is a discussion on a and (b or c) better than (a and b) or (a and c) ? within the SQL Server forums, part of the Microsoft SQL Server category; --> When I use "sql server enterprise manager" gui and write an expression like where a=1 and (b=2 or c=3) ...


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 05-16-2008, 02:39 PM
Leif Neland
 
Posts: n/a
Default a and (b or c) better than (a and b) or (a and c) ?

When I use "sql server enterprise manager" gui and write an expression like

where a=1 and (b=2 or c=3)

it gets transformed into

where (a=1 and b=2)
or (a=1 and c=3)

Is it only because it is easier to display the query in the query-design
table?

Or is it actually a more efficient method?

I.e, when I take the finished query and paste it into my asp-source,
should I keep the transformed query, which is larger, or transform it
back to the way I originally wrote it?

Leif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-16-2008, 02:39 PM
Dan Guzman
 
Posts: n/a
Default Re: a and (b or c) better than (a and b) or (a and c) ?

> Is it only because it is easier to display the query in the query-design
> table?


Yes, I believe EM changes the query to facilitate GUI display.

> Or is it actually a more efficient method?


SQL is declarative rather than procedural. The optimizer in the database
engine will try to generate the most efficient plan regardless of how the
query is expressed. If you compare the execution plans of the 2 queries in
Query Analyzer, I would expect both to be identical because they are
semantically identical.

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Leif Neland" <leif@neland.dk> wrote in message
news:482c078c$0$15887$edfadb0f@dtext01.news.tele.d k...
> When I use "sql server enterprise manager" gui and write an expression
> like
>
> where a=1 and (b=2 or c=3)
>
> it gets transformed into
>
> where (a=1 and b=2)
> or (a=1 and c=3)
>
> Is it only because it is easier to display the query in the query-design
> table?
>
> Or is it actually a more efficient method?
>
> I.e, when I take the finished query and paste it into my asp-source,
> should I keep the transformed query, which is larger, or transform it back
> to the way I originally wrote it?
>
> Leif


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-16-2008, 02:39 PM
--CELKO--
 
Posts: n/a
Default Re: a and (b or c) better than (a and b) or (a and c) ?

>> WHERE A = 1 AND (B = 2 OR C = 3)
it gets transformed into
WHERE (A = 1 AND B = 2)
OR (A = 1 AND C = 3) <<

I think that is weird, too. It should not make any difference because
the optimizer will figure it out. Hell, procedural languages can
optimize simple predicates like that easier these days. For what it is
worth, that is the Canonical Disjunctive Form in formal logic and you
can probably Google some very boring articles about it.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-16-2008, 02:39 PM
Shuurai
 
Posts: n/a
Default Re: a and (b or c) better than (a and b) or (a and c) ?


> When I use "sql server enterprise manager" gui and write an expression like
>
> where a=1 and (b=2 or c=3)
>
> it gets transformed into
>
> where (a=1 and b=2)
> * *or *(a=1 and c=3)
>
> Is it only because it is easier to display the query in the query-design
> table?


I believe this to be the case.

> Or is it actually a more efficient method?


They should be identical.

> I.e, when I take the finished query and paste it into my asp-source,
> should I keep the transformed query, which is larger, or transform it
> back to the way I originally wrote it?


I would keep your original logic, if for no other reason than it
probably makes more sense to you the way it is written. You should
not see any difference in performance, and certainly no difference in
results.
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 05:12 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