使用JOIN时WHERE子句vs ON [英] WHERE Clause vs ON when using JOIN

查看:186
本文介绍了使用JOIN时WHERE子句vs ON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下T-SQL代码:

Assuming that I have the following T-SQL code:

SELECT * FROM Foo f
INNER JOIN Bar b ON b.BarId = f.BarId;
WHERE b.IsApproved = 1;

以下代码也返回相同的行集:

The following one also returns the same set of rows:

SELECT * FROM Foo f
INNER JOIN Bar b ON (b.IsApproved = 1) AND (b.BarId = f.BarId);

这可能不是这里最好的示例,但是两者之间在性能上有什么区别吗?

This might not be the best case sample here but is there any performance difference between these two?

推荐答案

不,查询优化器足够聪明,可以为两个示例选择相同的执行计划.

No, the query optimizer is smart enough to choose the same execution plan for both examples.

您可以使用SHOWPLAN检查执行计划.

You can use SHOWPLAN to check the execution plan.

尽管如此,您应该将所有联接连接放在ON子句上,并将所有限制都放在WHERE子句上.

Nevertheless, you should put all join connection on the ON clause and all the restrictions on the WHERE clause.

这篇关于使用JOIN时WHERE子句vs ON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆