SQL Server 2005 - Order of Inner Joins

前端 未结 3 1020
渐次进展
渐次进展 2020-12-19 10:58

I have a query containing three inner join statements in the Where clause. The query takes roughly 2 minutes to execute. If I simply change the order of two of the inner joi

3条回答
  •  借酒劲吻你
    2020-12-19 11:24

    Because by changing the order of the joins, SQL Server is coming up with a different execution plan for your query (chances are it's changing the way it's filtering the tables based on your joins).

    In this case, I'm guessing you have several large tables...one of which performs the majority of the filtering.

    In one query, your joins are joining several of the large tables together and then filtering the records at the end.

    In the other, you are filtering the first table down to a much smaller sub-set of the data...and then joining the rest of the tables in. Since that initial table got filtered before joining the other large recordsets, performance is much better.

    You could always verify but running the query with the 'Show query plan' option enabled and see what the query plan is for the two different join orders.

提交回复
热议问题