Does Sql JOIN order affect performance?

后端 未结 6 522
忘掉有多难
忘掉有多难 2020-11-30 08:08

I was just tidying up some sql when I came across this query:

SELECT 
        jm.IMEI ,
        jm.MaxSpeedKM ,
              


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 08:59

    JOIN order doesn't matter, the query engine will reorganize their order based on statistics for indexes and other stuff.

    For test do the following:

    • select show actual execution plan and run first query
    • change JOIN order and now run the query again
    • compare execution plans

    They should be identical as the query engine will reorganize them according to other factors.

    As commented on other asnwer, you could use OPTION (FORCE ORDER) to use exactly the order you want but maybe it would not be the most efficient one.

    AS a general rule of thumb, JOIN order should be with table of least records on top, and most records last, as some DBMS engines the order can make a difference, as well as if the FORCE ORDER command was used to help limit the results.

提交回复
热议问题