不胜人生一场醉

欢迎来到不胜人生一场醉>>   | 首页 资源中心 | 软件开发 | 软件工程 | 艺术长廊 | 数据仓库专区 | 你的故事我的歌 | 数据库专区 | ITPUB论坛

Oracle学习系列—数据库优化—In和Exists的使用

发表人:PercyWang | 发表时间: 2007年五月11日, 01:18

关于InExists的使用优劣,官方文档<Oracle9iR2.Database.Performance.Tuning.Guide.and.Reference>有很好的说明.

实际上并不存在优劣之分,Oracle会跟据实际情况进行InExists的相互转换,因此可以说大多数情况下是等效的.


In certain circumstances, it is better to use IN rather than EXISTS. In general, if the selective predicate is in the subquery, then use IN. If the selective predicate is in the parent query, then use EXISTS.

在某种情况,使用In要比Exists更好一些.通常情况下,如果选择断言使用在子查询语句中,最好使用In;如果选择断言在父查询语句中,则最好使用Exists

Note: This discussion is most applicable in an OLTP environment,where the access paths either to the parent SQL or subquery are through indexed columns with high selectivity. In a DSS environment, there can be low selectivity in the parent SQL or subquery, and there might not be any indexes on the join columns. In a DSS environment, consider using semi-joins for the EXISTS case.

备注:这样的讨论在OLTP环境中更适用一些,通常情况下对父或子查询语句的访问通过具有良好选择性的索引列来进行的.而在DSS环境中,访问子或父查询语句都是比较的选择性的或者没有任何索引列.DSS环境中可以考虑使用Exists语句.

例如

SELECT /* EXISTS example */

e.employee_id

, e.first_name

, e.last_name

, e.salary

FROM employees e

WHERE EXISTS (SELECT 1 FROM orders o /* Note 1 */

WHERE e.employee_id = o.sales_rep_id /* Note 2 */

AND o.customer_id = 144); /* Note 3 */

将会被解析成

SELECT /* IN example */

e.employee_id

, e.first_name

, e.last_name

, e.salary

FROM employees e

WHERE e.employee_id IN (SELECT o.sales_rep_id /* Note 4 */

FROM orders o

WHERE o.customer_id = 144); /* Note 3 */

例如

SELECT /* IN example */

e.employee_id

, e.first_name

, e.last_name

, e.department_id

, e.salary

FROM employees e

WHERE e.department_id = 80 /* Note 5 */

AND e.job_id = 'SA_REP' /* Note 6 */

AND e.employee_id IN (SELECT o.sales_rep_id FROM orders o); /* Note 4 */

将会被解析成

SELECT /* EXISTS example */

e.employee_id

, e.first_name

, e.last_name

, e.salary

FROM employees e

WHERE e.department_id = 80 /* Note 5 */

AND e.job_id = 'SA_REP' /* Note 6 */

AND EXISTS (SELECT 1 /* Note 1 */

FROM orders o

WHERE e.employee_id = o.sales_rep_id); /* Note 2 */

发表评论

标题

在此添加评论

称呼

邮箱地址(可选)

个人主页(可选)


authimage



Valid XHTML 1.0 Strict and CSS. Powered by pLog
Design by Blog.lvwo.com