Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    4

    Unanswered: Join syntax question

    In a join, does it matter which table you identify first in 'join on' part?

    I'll post some code to explain my question better.
    Code:
    Select A.field10, B.field11
    FROM tbl_a as A
    Join tbl_b as B on (A.field1=B.field1 and A.field2=B.field2)
    Where A.Fileld1 = xxx
    And A.field2 = xxx
    and A.field3 = xxx
    Assuming that A.field1,field2,field3 is an index for Table A
    and B.field1, field2 is an index for Table B.

    Will the speed of the query be different if change the 'join on' part to this, where you change the order of the tables in your join ..

    Code:
    Select A.field10, B.field11
    FROM tbl_a as A
    Join tbl_b as B on (B.field1=A.field1 and B.field2=A.field2)
    Where A.Fileld1 = xxx
    And A.field2 = xxx
    and A.field3 = xxx
    So I've changed the order of the table...

    Code:
    From
    Join tbl_b as B on (A.field1=B.field1 and A.field2=B.field2)
    To
    Join tbl_b as B on (B.field1=A.field1 and B.field2=A.field2)
    Its seems in some of our queries that if we make this change it speeds up the query significantly. But I'm not sure if this is because of the ODBC Driver we're using or if it SHOULD speed up the query for some reason.

    Thanks for any input you can provide.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    if you run the same queries over and over, the execution plan and the data get cached. I am pretty this certain explains your improvement. Unless your SQL is egregiously bad, and this looks pretty vanilla if this is all there is (which I doubt), how you write your query is just a suggestion that gets interpreted and optimized.

    The real sql can have numerous real problems and I unaware of any ODBC drivers issues.

    Have you tried both queries in the Query Analyzer\Management Studio and running DBCC DROPCLEANBUFFERS and DBCC FREEPROCACHE between each execution. Read about them before you try them.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •