SQL Server 2005 - Order of Inner Joins

痞子三分冷 提交于 2019-11-29 10:58:28

SQL is declarative, that is, the JOIN order should not matter.

However it can in practice, say, if it's a complex query when the optimiser does not explore all options (which in theory could take months).

Another option is that it's a very different query if you reorder and you get different results, but this is usually with OUTER JOINs.

And it could also be the way the ON clause is specified It has to change if you reorder the FROM clause. Unless you are using the older (and bad) JOIN-in-the-WHERE-clause.

Finally, if it's a concern you could use parenthesis to change evaluation order to make your intentions clear, say, filter on a large table first to generate a derived table.

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.

I would have thought it was smart enough to do that as well, but clearly it's still performing the joins in the order you explicitly list them... As to why that affects the performance, if the first join produces an intermediate result set of only 100 records in one ordering scheme, then the second join will be from that 100-record set to the third table. If putting the other join first produces a first intermediate result set of one million records, then the second join will be from a one million row result set to the third table...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!