oracle中的exists 和not exists 用法
2010-11-15 00:30
267 查看
有两个简单例子,以说明 “exists”和“in”的效率问题
exists 用法:
请注意 1)句中的有颜色字体的部分 ,理解其含义;
其中
相当于一个关联表查询,相当于
但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。
“exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。
因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。
in 的用法:
继续引用上面的例子
第一句SQL语句的执行效率不如第二句。
通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。
1.1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; 2. 3. T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。 4. 5.2) select * from T1 where T1.a in (select T2.a from T2) ; 6. 7. T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。
exists 用法:
请注意 1)句中的有颜色字体的部分 ,理解其含义;
其中
“select 1 from T2 where T1.a=T2.a”
相当于一个关联表查询,相当于
“select 1 from T1,T2 where T1.a=T2.a”
但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。
“exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。
因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。
in 的用法:
继续引用上面的例子
“select * from T1 where T1.ticketid in (select T2.id from T2) ” Select name from employee where name not in (select name from student); Select name from employee where not exists (select name from student);
第一句SQL语句的执行效率不如第二句。
通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。
相关文章推荐
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解(转)
- oracle中的exists 和not exists 用法
- oracle中的exists 和not exists 用法详解
- oracle中in和exists、not in和not exists的用法与效率
- Oracle中的exists、not exists和in用法
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法
- oracle中的exists 和not exists 用法
- oracle exists&not exists用法及与in&not in比较(10g)-开发系列(四)
- oracle中的exists 和not exists 用法详解