【发布时间】:2015-08-26 10:33:37
【问题描述】:
假设我们有 2 个表 - EMP 和 DEPT。 EMP 可以在 department_id 列上与 DEPT 连接(包含在两个表中)
让我们假设 EMP 表有 1000 名员工。其中 100 人的工资低于 1000。让我们假设任何列上都没有索引。
我可以编写一个查询以三种不同的方式获取员工。是否有任何选项可以让我获得性能优势-
1
select * from emp i, dept h
where i.department_id = h.department_id
and i.salary <1000;
2
select * from emp i join dept h
on i.department_id = h.department_id
and i.salary <1000;
3
select * from emp i join dept h
on i.department_id = h.department_id
where i.salary <1000;
我尝试了 3 个查询的解释计划,所有查询都是相同的。从逻辑上讲,我认为所有人都应该提供相同的性能。
【问题讨论】:
-
2 和 3 的区别是什么?
-
在2中,有'and'。 3,有“哪里”。我不知道任何逻辑上的区别。
-
所以结论是?我的建议是始终使用新样式、明确的 JOIN 语法。更容易编写,更容易阅读,如果需要,更容易转换为外连接!
-
1 是我使用并看到使用的查询。
-
我会说使用查询 3。查询 1 使用较旧的隐式连接语法。 (我上面的评论描述了为什么你应该做显式连接。)