SQL Server 2005 - Order of Inner Joins

前端 未结 3 1027
渐次进展
渐次进展 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:16

    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.

提交回复
热议问题