The most optimal order (join) for the left join

I have 3 tables, Table1 (with 1020690 records), Table2 (with 289425 records), Table 3 (with 83692 records). I have something like this

SELECT * FROM Table1 T1 /* OK fine select * is bad when not all columns are needed, this is just an example*/
LEFT JOIN Table2 T2 ON T1.id=T2.id
LEFT JOIN Table3 T3 ON T1.id=T3.id

and a query like this

SELECT * FROM Table1 T1
LEFT JOIN Table3 T3 ON T1.id=T3.id
LEFT JOIN Table2 T2 ON T1.id=T2.id

The query plan shows that it uses 2 Merge Join for both connections. For the first query, the first merge occurs with T1 and T2, and then with T3. For the second query, the first merge occurs with T1 and T3, and then with T2.

Both of these queries take about the same time (40 seconds), or sometimes Query1 takes a couple of seconds longer.

So my question is: does it have a join order?

+3
source share
3 answers

, , . , .

- , SQL. , , . MS SQL Server, .

, 40 . , . , SQL Server Profiler SQL Server Database Engine Tuning , , .

+3

, , , . . , , . . . , FORCE ORDER .

, . , , , , . : , , FORCE ORDER , , , .

+2

, SQL Server , , , . , , , .

+1

Source: https://habr.com/ru/post/1749660/


All Articles