ITPub博客

首页 > 数据库 > Oracle > 数据库中exists(not exists)  和  in (not in) 的使用

数据库中exists(not exists)  和  in (not in) 的使用

Oracle 作者:BrianZJJ 时间:2013-04-12 09:40:56 0 删除 编辑

一、两者的语法比较

  

    in 与 exist 的语法比较:
      select * from 数据表 t where t.x in (...)
      括号内可以是符合t.x字段类型的值集合,如('1','2','3'),但如果t.x是number类型的时候,似乎这样的写法会出问题;也可以是通过另外的select语句查询出来的值集合,如(select y from 数据表2 where ...)。

      select * from 数据表 t where [...] and exist (...)
      方括号内为其它的查询条件,可以没有。exist后面的括号内可以是任意的条件,这个条件可以与外面的查询没有任何关系,也可以与外面的条件结合。如:(select * from 数据表2 where 1=1) 或 (select * from 数据表2 where y=t.x)

 

实例:

in的SQL语句
        SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
        FROM tab_oa_pub WHERE is_check=1 and
        category_id in (select id from tab_oa_pub_cate where no='1')
        order by begintime desc

exists的SQL语句
         SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
         FROM tab_oa_pub WHERE is_check=1 and
         exists (select id from tab_oa_pub_cate where      tab_oa_pub.category_id=convert   (int,no)  and  no='1')
order by begintime desc

二、效率比较:

    先讨论IN和EXISTS。
        select * from t1 where x in ( select y from t2 )
        事实上可以理解为:
       select * from t1, ( select distinct y from t2 ) t2 where t1.x = t2.y
       如果你有一定的SQL优化经验,从这句很自然的可以想到t2绝对不能是个大表,因为需要对t2进行全表的“唯一排序”,如果t2很大这个排序的性能是不可忍受的。但是t1可以很大,为什么呢?最通俗的理解就是因为t1.x=t2.y可以走索引。但这并不是一个很好的解释。试想,如果t1.x和t2.y都有索引,我们知道索引是种有序的结构,因此t1和t2之间最佳的方案是走merge join。另外,如果t2.y上有索引,对t2的排序性能也有很大提高。

            select * from t1 where exists ( select null from t2 where y = x )
        可以理解为:
            for x in ( select * from t1 )
            loop
                if ( exists ( select null from t2 where y = x.x ) then
                   OUTPUT THE RECORD!
                end if
            end loop
     这个更容易理解,t1永远是个表扫描!因此t1绝对不能是个大表,而t2可以很大,因为y=x.x可以走t2.y的索引。

        综合以上对IN/EXISTS的讨论,我们可以得出一个基本通用的结论:IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。

 

 

再举两个简单例子,以说明 “exists”和“in”的效率问题

1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;

T1数据量小而T2数据量非常大时,T1<时,1) 的查询效率高。

2) select * from T1 where T1.a in (select T2.a from T2) ;

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 的用法:

继续引用上面的例子

“2) select * from T1 where T1.a in (select T2.a from T2) ”

这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。

打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用 2)的写法就可以这样:

“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通常查询速度快的原因

 

 

三、总结

--------------------------------------------------------------------------------------------------

1、in和exist的主要区别体现在对sql执行计划的影响上。
传统上认为,如果子查询的条件更具选择性(selective),就用in;而如果父查询(外层查询)的条件更具选择性(selective),就用exist。

2、这个exists好像必须是where后面的条件弄成两表连接才行
1:not exists(not in)

 not exists:

   这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的结果集.也就是说,

返回的结果集中不存在d.deptno=10结果集的记录,即emp表中没有dept表中d.deptno=10的记录.

 SQL> select empno,ename,deptno from scott.emp e where not exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME      DEPTNO
----- ---------- ------
 7369 SMITH          20
 7499 ALLEN          30
 7521 WARD           30
 7566 JONES          20
 7654 MARTIN         30
 7698 BLAKE          30
 7788 SCOTT          20
 7844 TURNER         30
 7876 ADAMS          20
 7900 JAMES          30
 7902 FORD           20

11 rows selected

not in:

第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).

SQL> select empno,ename,deptno from scott.emp e where deptno not in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME      DEPTNO
----- ---------- ------
 7369 SMITH          20
 7499 ALLEN          30
 7521 WARD           30
 7566 JONES          20
 7654 MARTIN         30
 7698 BLAKE          30
 7788 SCOTT          20
 7844 TURNER         30
 7876 ADAMS          20
 7900 JAMES          30
 7902 FORD           20

11 rows selected

2: exists(in)

  exists:

   这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的记录结果集.

也就是说返回的结果集中只存在有d.deptno=10的记录,即emp表中只存在dept表中d.deptno=10的记录.

SQL> select empno,ename,deptno from scott.emp e where  exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME      DEPTNO
----- ---------- ------
 7782 CLARK          10
 7839 KING           10
 7934 MILLER         10

in:

第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).

SQL> select empno,ename,deptno from scott.emp e where  deptno in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME      DEPTNO
----- ---------- ------
 7782 CLARK          10
 7839 KING           10
 7934 MILLER         10

 

  oracle 中exists (in)和not exists(not in)是判断是否存在和不存在表中记录的关键子.

 请注意:not in 逻辑上不完全等同于not exists,如果你误用了not in,小心你的程序存在致命的BUG:因此,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。


补充:1、not in 在使用过程中要注意 not in() 括号中 的select 语句查询的值不包含null值,否则会查不到数据; 如:where incode not in ( select upper_id from tableaada )  其中括号中的upper_id 不能包含null值,然而in() 可以 。

     2、exists 的含义是:表示是否存在符合某种条件的记录,及是否符合括号中的条件

如: select * from table1 where exists ( select upper_id from table2 ) 意为:在table2中upper_id 在table1 中是否有对应的记录。注意 where exists  之间不能加入 某个字段 否则会出现错误。

 

 

 

 

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23742204/viewspace-1115449/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-04-17