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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leif Neland

    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
  • Dan Guzman

    #2
    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


    "Leif Neland" <leif@neland.dk wrote in message
    news:482c078c$0 $15887$edfadb0f @dtext01.news.t ele.dk...
    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

    Comment

    • --CELKO--

      #3
      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.

      Comment

      • Shuurai

        #4
        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.

        Comment

        Working...