Thread: Join syntax question
06-08-09, 18:43 #1Registered User
- Join Date
- May 2009
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.
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
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 ..
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
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)
Thanks for any input you can provide.
06-08-09, 19:33 #2Annie's Dog Walker
Provided Answers: 6
- Join Date
- Nov 2004
- on the wrong server
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.