我们在查询中使用 join 的顺序是否会影响其执行时间?

发布于 2024-08-21 23:07:59 字数 35 浏览 7 评论 0原文

我们在查询中使用 join 的顺序是否会影响其执行时间?

Does the sequence in which we use join in a query effects its execution time ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-08-28 23:07:59

不,事实并非如此。

SQL Server 的优化器会选择最佳(它认为)的方式,而不管连接顺序如何。

SQL Server 支持特殊的提示,FORCE ORDER,它使表按照它们列出的顺序在连接中引导。

这些查询:

SELECT  *
FROM    t_a
JOIN    t_b
ON      a = b
OPTION (FORCE ORDER)

SELECT  *
FROM    t_b
JOIN    t_a
ON      a = b
OPTION (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:

SELECT  *
FROM    t_a
JOIN    t_b
ON      a = b
OPTION (FORCE ORDER)

and

SELECT  *
FROM    t_b
JOIN    t_a
ON      a = b
OPTION (FORCE ORDER)

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.

憧憬巴黎街头的黎明 2024-08-28 23:07:59

是的,确实如此。其效果可以在查询执行计划中看到。请参阅 这个
另一个链接是 此处

Yes it does. Its effect can be seen in the query execution plan. Refer this and this.
Another link is here

一页 2024-08-28 23:07:59

优化器通常会比较所有连接顺序并尝试选择最佳顺序,而不管您编写查询的顺序如何,但是在一些复杂的查询中,需要考虑的选项太多,请注意,可能的连接顺序的数量会增加到加入的表的数量。在这种情况下,优化器不会考虑所有选项,但肯定会考虑您在查询中编写联接的顺序,因此可能会影响执行计划和执行时间。

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.

鸵鸟症 2024-08-28 23:07:59

对于外连接,表的顺序会改变查询的含义,并且很可能会改变执行计划。

For outer joins, the order of tables changes the meaning of your query and is very likely to change the execution plan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文