zoukankan      html  css  js  c++  java
  • 子查询解嵌套not in 无法展开改写

    SQL> explain plan for select *
      from OPS$CZTEST1.SAVJ_ATOMJOURBAK
     where ((list_flag = '1' and prt_flag = '0') and
           acct_no not in
           (select acct_no
               from OPS$CZTEST1.savb_basicinfo1
              where ((card_no is not null and base_acct_no is null) or
                    (book_flag = '1' and bus_code = 21))));  2    3    4    5    6    7    8 
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 3909253513
    
    --------------------------------------------------------------------------------------
    | Id  | Operation                             | Name                         | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |                                       | 54953 |    18M|   594M  (1)      |999:59:59 |
    |*  1 |  FILTER                               |                                       |             |            |                           |          |
    |*  2 |   TABLE ACCESS FULL   | SAVJ_ATOMJOUR0  | 54954 |    18M|  1042   (3)       | 00:00:13 |
    |*  3 |   TABLE ACCESS FULL  | SAVB_BASICINFO1   |     1       |    73   | 11220   (1)     | 00:02:15 |
    --------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter( NOT EXISTS (SELECT /*+ */ 0 FROM
                  "OPS$CZTEST1"."SAVB_BASICINFO1" "SAVB_BASICINFO1" WHERE ("BASE_ACCT_NO" IS
                  NULL AND "CARD_NO" IS NOT NULL OR "BUS_CODE"=21 AND "BOOK_FLAG"='1') AND
                  LNNVL("ACCT_NO"<>:B1)))
       2 - filter("PRT_FLAG"='0' AND "LIST_FLAG"='1')
       3 - filter(("BASE_ACCT_NO" IS NULL AND "CARD_NO" IS NOT NULL OR
                  "BUS_CODE"=21 AND "BOOK_FLAG"='1') AND LNNVL("ACCT_NO"<>:B1))
    
    21 rows selected.
    
    
    可以看到这里:
    ( NOT EXISTS (SELECT /*+ */ 0 FROM
                  "OPS$CZTEST1"."SAVB_BASICINFO1" "SAVB_BASICINFO1" WHERE ("BASE_ACCT_NO" IS
                  NULL AND "CARD_NO" IS NOT NULL OR "BUS_CODE"=21 AND "BOOK_FLAG"='1') AND
                  LNNVL("ACCT_NO"<>:B1)))
    
    子查询没有展开,FILTER 循环类似于NL循环,会导致表SAVB_BASICINFO1访问多次,not in 可以改写成左关联的形式,改写如下:
    SQL> explain plan for select count(*)
      from OPS$CZTEST1.SAVJ_ATOMJOURBAK a,
           (select acct_no
              from OPS$CZTEST1.savb_basicinfo1
             where (card_no is not null and base_acct_no is null)
            or
              (book_flag = '1' and bus_code = 21)) b
     where a.list_flag = '1'
       and a.prt_flag = '0'
       and a.acct_no = b.acct_no(+)
       and b.acct_no is null;  2    3    4    5    6    7    8    9   10   11  
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 4182698075
    
    -----------------------------------------------------------------------------------------------
    | Id  | Operation                           | Name                          | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
    -----------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |                                       |     1      |   110 |       | 13060   (1)| 00:02:37 |
    |   1 |  SORT AGGREGATE        |                                      |     1       |   110 |       |            |          |
    |*  2 |   HASH JOIN ANTI            |                                     |             1 |   110 |  2632K| 13060   (1)| 00:02:37 |
    |*  3 |    TABLE ACCESS FULL | SAVJ_ATOMJOUR0  | 54954 |  1985K|       |  1041   (2)| 00:00:13 |
    |*  4 |    TABLE ACCESS FULL | SAVB_BASICINFO1 |   166K|    11M|       | 11216   (1)| 00:02:15 |
    -----------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("ACCT_NO"="ACCT_NO")
       3 - filter("PRT_FLAG"='0' AND "LIST_FLAG"='1')
       4 - filter("BASE_ACCT_NO" IS NULL AND "CARD_NO" IS NOT NULL OR "BUS_CODE"=21 AND
                  "BOOK_FLAG"='1')
    
    19 rows selected.
    
     
    
     
    
     
    


     

  • 相关阅读:
    Using PL/SQL APIs as Web Services
    1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码
    我对贝叶斯分类器的理解
    VB.NET的前世今生
    《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用
    网络流合集:bzoj1433,1934,1854 题解
    Android 下拉刷新上拉载入 多种应用场景 超级大放送(上)
    Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
    深入浅出之数据分析四步曲
    深入浅出之数据分析四步曲
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/3798114.html
Copyright © 2011-2022 走看看