我们在查询中使用 join 的顺序是否会影响其执行时间?
我们在查询中使用 join 的顺序是否会影响其执行时间?
Does the sequence in which we use join in a query effects its execution time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,事实并非如此。
SQL Server
的优化器会选择最佳(它认为)的方式,而不管连接顺序如何。SQL Server
支持特殊的提示,FORCE ORDER
,它使表按照它们列出的顺序在连接中引导。这些查询:
和
将生成相同的计划,但省略了 OPTION (FORCE ORDER) 并添加了不同的计划。
但是,只有当您完全确定自己知道自己在做什么时才应该使用此提示。
No, it does not.
SQL Server
's optimizer picks the best (in its opinion) way regardless on the join order.SQL Server
supports a special hint,FORCE ORDER
, which makes the tables to lead in the joins in the order they are listed.These queries:
and
will produce identical plans with the
OPTION (FORCE ORDER)
omitted and different plans with that added.However, you should use this hint only when you absolutely sure you know what you are doing.
是的,确实如此。其效果可以在查询执行计划中看到。请参阅 此 和 这个。
另一个链接是 此处
Yes it does. Its effect can be seen in the query execution plan. Refer this and this.
Another link is here
优化器通常会比较所有连接顺序并尝试选择最佳顺序,而不管您编写查询的顺序如何,但是在一些复杂的查询中,需要考虑的选项太多,请注意,可能的连接顺序的数量会增加到加入的表的数量。在这种情况下,优化器不会考虑所有选项,但肯定会考虑您在查询中编写联接的顺序,因此可能会影响执行计划和执行时间。
The optimizer will usually compare all join orders and try to select the optimal order regardless of the order you wrote the query however in some complicated queries there are simply too many options to consider, note that the number of possible join orders increases to the factorial of the number of tables joined. In this case the optimizer will not consider all options but will definitely consider the order you wrote the joins in the query and therefor may effect execution plan and execution time.
对于外连接,表的顺序会改变查询的含义,并且很可能会改变执行计划。
For outer joins, the order of tables changes the meaning of your query and is very likely to change the execution plan.