WHERE Clause vs ON when using JOIN(使用 JOIN 时的 WHERE 子句与 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 子句与 ON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JOIN 时的 WHERE 子句与 ON
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
