Oracle Exists用法
来源:互联网 发布:小米清空用户数据失败 编辑:程序博客网 时间:2024/06/05 19:52
Oracle Exists用法
(一) 用Oracle Exists替换DISTINCT:
当提交一个包含一对多表信息(比如部门表和雇员表)的查询时,避免在SELECT子句中使用DISTINCT。一般能够考虑用Oracle EXIST替换,Oracle Exists使查询更为迅速,因为RDBMS核心模块将在子查询的条件一旦满足后,立即返回结果。
例子:
SELECT DISTINCT DEPT_NO,DEPT_NAME FROM DEPT D,EMP E WHERE D.DEPT_NO = E.DEPT_NO;
SELECT DEPT_NO,DEPT_NAME FROM DEPT D WHERE Exists (SELECT ‘X' FROM EMP E WHERE E.DEPT_NO = D.DEPT_NO);
二)exists和in的效率问题:
使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行 子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。 这也就是使用EXISTS比使用IN通常查询速度快的原因。
1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
2) select * from T1 where T1.a in (select T2.a from T2) ;
T1数据量小而T2数据量非常大时,T1<
T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。
1)Select name from employee where name not in (select name from student);
2)Select name from employee where not exists (select name from student);
第一句SQL语句的执行效率不如第二句。
二
例: select * from emp_tax;
1: 内表必须要和外表连接。
select *
from emp_tax o
where exists (select *
from emp_tax i
where i.empno = o.empno
and i.empno < 0005);
2. exists 适合外表的结果集小的情况。因为exists是对外表作loop,每次loop再对那表进行查询。
当 exists 中的 where 后面条件为真的时候则把前面select 的内容显示出来(外表的select ).
Oracle exists 和 in 的真正区别
in 是把外表和那表作hash join,而exists是对外表作loop,每次loop再对那表进行查询。
这样的话,in适合内外表都很大的情况,exists适合外表结果集很小的情况。
0 0
- oracle exists 用法
- oracle exists 用法
- 讨论Oracle Exists用法
- 讨论Oracle Exists用法
- Oracle Exists用法
- Oracle Exists 用法
- Oracle Exists用法
- Oracle Exists 用法
- oracle中exists用法
- Oracle Exists 用法
- oracle EXISTS语句用法
- Oracle Exists用法
- oracle sql exists 用法
- Oracle Exists用法
- oracle exists用法解析
- oracle中的exists 和not exists 用法
- oracle中的exists 和not exists 用法
- oracle中的exists 和not exists 用法
- C#中Bind和Eval的区别
- 凸优化问题简单概念
- Method public java.lang.String org.apache.commons.lang.exception.NestableRuntimeException.getMessage
- pom文件报错读取架包失败Missing很多架包但是架包都有
- BottomNavigationBar的使用
- Oracle Exists用法
- 在AndroidStudio直接运行时百度地图显示,打包成APK,安装在真机上不显示的问题
- [练习]traceroute命令
- 如何打开pr_debug调试信息
- JAVA & JSON详解
- iOS开发:隐藏状态栏的方法
- linux共享内存初体验
- react native Slider 使用详解
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)