not in과 not exists의 차이와 사용법을 자세히 설명하십시오 (not in의 성능은 나쁘지 않습니다!)

11883 단어
        oracle not exists not in     ,           ,         ,          。         ,             ,         :not in     not exists ,          。  
      
       
      
            ,    :  
      
    create table  ljn_test1 (col number);  
      
    create table  ljn_test2 (col number);  
      
            :  
      
    insert into ljn_test1  
      
    select level from dual connect by level <=30000;  
      
    insert into ljn_test2  
      
    select level+1 from dual connect by level <=30000;  
      
    commit;  
      
              not exists not in     :  
      
    select * from ljn_test1 where not exists (select 1 from ljn_test2 where ljn_test1.col = ljn_test2.col);  
      
       
      
           COL  
      
    ----------  
      
             1  
      
       
      
    Elapsed: 00:00:00.06  
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
           COL  
      
    ----------  
      
             1  
      
       
      
    Elapsed: 00:00:21.28  
      
        ,  not exists  0.06 ,   not in  21 ,  3    !    ?       ,    SQL        。  
      
                 ,      :  
      
    truncate table ljn_test1;  
      
    truncate table ljn_test2;  
      
    insert into ljn_test1 values(1);  
      
    insert into ljn_test1 values(2);  
      
    insert into ljn_test1 values(3);  
      
    insert into ljn_test2 values(2);  
      
    insert into ljn_test2 values(null);  
      
    commit;  
      
            SQL:  
      
    select * from ljn_test1 where not exists (select 1 from ljn_test2 where ljn_test1.col = ljn_test2.col);  
      
       
      
           COL  
      
    ----------  
      
             3  
      
             1  
      
       
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
    no rows selected  
      
      not in      ,        。         :  
      
    A.  select * from ljn_test1 where col not in (select col from ljn_test2);  
      
    A              B:  
      
    B.  select * from ljn_test1 where col not in (2,null);  
      
    B           C:  
      
    C.  select * from ljn_test1 where col <> 2 and col <> null;  
      
      col <> null      ,                。  
      
            :  not in         ,          !  
      
    not exists          ,  not exists      ljn_test1 ljn_test2   ,null         ,  ljn_test2 col                  。  
      
           ljn_test1    ,ljn_test2    。  
      
          ,           :  
      
    1、  not exists  ,               ;  not in  ,                  。  
      
    2、  not exists  ,      ,              ;  not in  ,      ,               ,        。  
      
       
      
        ,              not in   not exists         。  
      
    not exists                ,                  , CBO(        )         hash join,            ,         :  
      
    set autot on;  
      
    select * from ljn_test1 where not exists (select 1 from ljn_test2 where ljn_test1.col = ljn_test2.col);  
      
       
      
           COL  
      
    ----------  
      
             3  
      
             1  
      
       
      
    Elapsed: 00:00:00.01  
      
       
      
    Execution Plan  
      
    ----------------------------------------------------------  
      
    Plan hash value: 385135874  
      
       
      
    --------------------------------------------------------------------------------  
      
    | Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |  
      
    --------------------------------------------------------------------------------  
      
    |   0 | SELECT STATEMENT   |           |     3 |    78 |     7  (15)| 00:00:01 |  
      
    |*  1 |  HASH JOIN ANTI    |           |     3 |    78 |     7  (15)| 00:00:01 |  
      
    |   2 |   TABLE ACCESS FULL| LJN_TEST1 |     3 |    39 |     3   (0)| 00:00:01 |  
      
    |   3 |   TABLE ACCESS FULL| LJN_TEST2 |     2 |    26 |     3   (0)| 00:00:01 |  
      
    --------------------------------------------------------------------------------  
      
       
      
    Predicate Information (identified by operation id):  
      
    ---------------------------------------------------  
      
       
      
       1 - access("LJN_TEST1"."COL"="LJN_TEST2"."COL")  
      
       
      
             ,         ,    not in:  
      
       
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
    no rows selected  
      
       
      
    Elapsed: 00:00:00.01  
      
       
      
    Execution Plan  
      
    ----------------------------------------------------------  
      
    Plan hash value: 3267714838  
      
       
      
    --------------------------------------------------------------------------------  
      
    | Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |  
      
    --------------------------------------------------------------------------------  
      
    |   0 | SELECT STATEMENT   |           |     1 |    13 |     5   (0)| 00:00:01 |  
      
    |*  1 |  FILTER            |           |       |       |            |          |  
      
    |   2 |   TABLE ACCESS FULL| LJN_TEST1 |     3 |    39 |     3   (0)| 00:00:01 |  
      
    |*  3 |   TABLE ACCESS FULL| LJN_TEST2 |     2 |    26 |     2   (0)| 00:00:01 |  
      
    --------------------------------------------------------------------------------  
      
       
      
    Predicate Information (identified by operation id):  
      
    ---------------------------------------------------  
      
       
      
       1 - filter( NOT EXISTS (SELECT 0 FROM "LJN_TEST2" "LJN_TEST2"  
      
                  WHERE LNNVL("COL"<>:B1)))  
      
       3 - filter(LNNVL("COL"<>:B1))  
      
       
      
             filter,          nested loop,        ,         。   not in    hash join       ?       ,                       hash join     ,  hash join        hash  ,                   ,           ,         ,  oracle      ,     ,  filter  。  
      
       
      
                     ,   :LNNVL("COL"<>:B1),  LNNVL           :http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions078.htm  
      
              ,oracle    filter    ,        ljn_test2 ,   LNNVL   ljn_test2.col    null ,     null ,             ,            ,  oracle        ,          filter     。  
      
              ,        :  
      
    truncate table ljn_test1;  
      
    truncate table ljn_test2;  
      
    insert into ljn_test1  
      
    select level from dual connect by level <=30000;  
      
    insert into ljn_test2  
      
    select level+1 from dual connect by level <=30000;  
      
    commit;  
      
          oracle     ljn_test2.col null     ,                ,                         :  
      
    select col from ljn_test2 where rowid=(select min(rowid) from ljn_test2);  
      
       
      
           COL  
      
    ----------  
      
          1982  
      
                :  
      
    update ljn_test2 set col = null where col=1982;  
      
    commit;  
      
           not in     :  
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
    no rows selected  
      
       
      
    Elapsed: 00:00:00.17  
      
       
      
              ,          21       !  
      
      ,        oracle          null ,      :  
      
    update ljn_test2 set col = 1982 where col is null;  
      
    select col from ljn_test2 where rowid=(select max(rowid) from ljn_test2);  
      
       
      
           COL  
      
    ----------  
      
         30001  
      
    update ljn_test2 set col = null where col=30001;  
      
    commit;  
      
        not in     :  
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
           COL  
      
    ----------  
      
             1  
      
       
      
    Elapsed: 00:00:21.11  
      
       not in        !  
      
          ,              , LNNVL          ,         !  
      
            ,      ,         !                 not exists       ,     oracle                 。  
      
           :  
      
                     :  
      
    alter table ljn_test1 modify col not null;  
      
    alter table ljn_test2 modify col not null;  
      
      ,        :  
      
    set autot on;  
      
    select * from ljn_test1 where col not in (select col from ljn_test2);  
      
       
      
           COL  
      
    ----------  
      
             1  
      
       
      
    Elapsed: 00:00:00.07  
      
       
      
    Execution Plan  
      
    ----------------------------------------------------------  
      
    Plan hash value: 385135874  
      
       
      
    --------------------------------------------------------------------------------  
      
    | Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |  
      
    --------------------------------------------------------------------------------  
      
    |   0 | SELECT STATEMENT   |           |     1 |    26 |    28   (8)| 00:00:01 |  
      
    |*  1 |  HASH JOIN ANTI    |           |     1 |    26 |    28   (8)| 00:00:01 |  
      
    |   2 |   TABLE ACCESS FULL| LJN_TEST1 | 30000 |   380K|    13   (0)| 00:00:01 |  
      
    |   3 |   TABLE ACCESS FULL| LJN_TEST2 | 30000 |   380K|    13   (0)| 00:00:01 |  
      
    --------------------------------------------------------------------------------  
      
       
      
    Predicate Information (identified by operation id):  
      
    ---------------------------------------------------  
      
       
      
       1 - access("COL"="COL")  
      
       
      
      !  oracle      hash join !             ,                ,      :  
      
           :  
      
                  。  
      
                 :  
      
    alter table ljn_test1 modify col null;  
      
    alter table ljn_test2 modify col null;  
      
          :  
      
    select * from ljn_test1 where col is not null and col not in (select col from ljn_test2 where col is not null);  
      
       
      
           COL  
      
    ----------  
      
             1  
      
       
      
    Elapsed: 00:00:00.07  
      
       
      
    Execution Plan  
      
    ----------------------------------------------------------  
      
    Plan hash value: 385135874  
      
       
      
    --------------------------------------------------------------------------------  
      
    | Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |  
      
    --------------------------------------------------------------------------------  
      
    |   0 | SELECT STATEMENT   |           |     1 |    26 |    28   (8)| 00:00:01 |  
      
    |*  1 |  HASH JOIN ANTI    |           |     1 |    26 |    28   (8)| 00:00:01 |  
      
    |*  2 |   TABLE ACCESS FULL| LJN_TEST1 | 30000 |   380K|    13   (0)| 00:00:01 |  
      
    |*  3 |   TABLE ACCESS FULL| LJN_TEST2 | 30000 |   380K|    13   (0)| 00:00:01 |  
      
    --------------------------------------------------------------------------------  
      
       
      
    Predicate Information (identified by operation id):  
      
    ---------------------------------------------------  
      
       
      
       1 - access("COL"="COL")  
      
       2 - filter("COL" IS NOT NULL)  
      
       3 - filter("COL" IS NOT NULL)  
      
       
      
    OK! hash join   !     not exists not in          。

좋은 웹페이지 즐겨찾기