1. 程式人生 > >優化案例2-----or 等價改寫 union all 的經典案例

優化案例2-----or 等價改寫 union all 的經典案例

    今天一個之前幫他優化過很多SQL的哥們找我說,有一個分頁SQL 很慢,程式debug監控到:前端顯示出來第一頁的資料就要30秒鐘,一共要顯示1萬2000多條資料,在PL SQL DEV裡面 手工執行它的SQL ,全出刷出來就需要大概20多秒的時間。讓我幫他優化一下。

    原SQL語句如下:

select C_ID,
       C_CONTENT,
       C_CODE,
       C_KEY_WORD,
       C_PHRASE,
       lb,
       C_PUBLISH,
       FK_CREATE_ID,
       C_CREATE_TIME,
       FK_DEPT_ID,
       C_STATUS,
       C_TYPE,
       jgmc,
       bbname
  from (select temp.*
          from (select ST.C_ID,
                       ST.C_CONTENT,
                       ST.C_CODE,
                       ST.C_KEY_WORD,
                       ST.C_PHRASE,
                       decode(tmp.c_type,
                              1,
                              '崗位勝任能力',
                              2,
                              '職業技能鑑定',
                              3,
                              '技能專家選聘',
                              4,
                              '專項考試',
                              5,
                              '專業技術資格',
                              '') as lb,
                       ST.C_PUBLISH,
                       ST.FK_CREATE_ID,
                       ST.C_CREATE_TIME,
                       ST.FK_DEPT_ID,
                       ST.C_STATUS,
                       ST.C_TYPE,
                       jg.jgmc,
                       tmp.c_name as bbname
                  from t_ksgl_st st
                  left join (select bbst.fk_st_id, bb.c_name, bb.c_type
                              from T_KSGL_TKBBST bbst
                              join t_ksgl_tkbb bb
                                on bb.c_id = bbst.fk_tkbb_id) tmp
                    on st.c_id = tmp.fk_st_id
                  left join dm_gy_jg jg
                    on st.fk_dept_id = jg.id
                 where st.c_deleted = 'N'
                   and ((st.c_publish = '1' and exists
                        (select *
                            from (select jg.id
                                    from dm_gy_jg jg
                                   where (jg.dw_id = '7f304de353c358egcfg267f023db8f25' and jg.jgsx = '1')
                                      or jg.id = '7f304de353c358egcfg267f023db8f25') t
                           where t.id = st.fk_dept_id)) or
                       (st.c_publish = '0' and st.fk_create_id = '80ec73e9e66e4153a70a209cd68c17c4'))
                 ) temp)
 group by C_ID,
          C_CONTENT,
          C_CODE,
          C_KEY_WORD,
          C_PHRASE,
          lb,
          C_PUBLISH,
          FK_CREATE_ID,
          C_CREATE_TIME,
          FK_DEPT_ID,
          C_STATUS,
          C_TYPE,
          jgmc,
          bbname
 order by c_create_time desc

      執行計劃為:

---------------------------------------------------------------------------------------------------
| Id   | Operation                       | Name          | Rows    | Bytes     | Cost  | Time     |
---------------------------------------------------------------------------------------------------
|    0 | SELECT STATEMENT                |               |   20897 |   7940860 | 93715 | 00:18:45 |
|    1 |   SORT GROUP BY                 |               |   20897 |   7940860 | 93715 | 00:18:45 |
|  * 2 |    FILTER                       |               |         |           |       |          |
|  * 3 |     HASH JOIN RIGHT OUTER       |               |  670394 | 254749720 | 39509 | 00:07:55 |
|    4 |      VIEW                       |               | 1302361 |  82048743 |  5325 | 00:01:04 |
|  * 5 |       HASH JOIN                 |               | 1302361 | 168004569 |  5325 | 00:01:04 |
|    6 |        TABLE ACCESS FULL        | T_KSGL_TKBB   |    3421 |    215523 |    27 | 00:00:01 |
|    7 |        TABLE ACCESS FULL        | T_KSGL_TKBBST | 1304287 |  86082942 |  5293 | 00:01:04 |
|  * 8 |      HASH JOIN RIGHT OUTER      |               |  453444 | 143741748 | 22504 | 00:04:31 |
|    9 |       TABLE ACCESS FULL         | DM_GY_JG      |   48328 |   2174760 |   635 | 00:00:08 |
| * 10 |       TABLE ACCESS FULL         | T_KSGL_ST     |  453444 | 123336768 | 15648 | 00:03:08 |
| * 11 |     TABLE ACCESS BY INDEX ROWID | DM_GY_JG      |       1 |        68 |     2 | 00:00:01 |
| * 12 |      INDEX UNIQUE SCAN          | PK_DM_GY_JG   |       1 |           |     1 | 00:00:01 |
---------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
------------------------------------------
* 2 - filter("ST"."FK_CREATE_ID"='80ec73e9e66e4153a70a209cd68c17c4' AND "ST"."C_PUBLISH"='0' OR "ST"."C_PUBLISH"='1' AND EXISTS (SELECT 0 FROM "DM_GY_JG" "JG" WHERE "JG"."ID"=:B1 AND
  ("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25' AND "JG"."JGSX"='1' OR "JG"."ID"='7f304de353c358egcfg267f023db8f25')))
* 3 - access("ST"."C_ID"="TMP"."FK_ST_ID"(+))
* 5 - access("BB"."C_ID"="BBST"."FK_TKBB_ID")
* 8 - access("ST"."FK_DEPT_ID"="JG"."ID"(+))
* 10 - filter("ST"."C_DELETED"='N')
* 11 - filter("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25' AND "JG"."JGSX"='1' OR "JG"."ID"='7f304de353c358egcfg267f023db8f25')
* 12 - access("JG"."ID"=:B1)

     其實主體部分就是temp 括號裡面的部分,分頁框架是外面程式套的,但是由於最終需要Group by ,那麼只能查詢出來所有的資料到temp中,然後彙總Group by,這個分頁是不能徹底優化的,只有去抓主要的主體部分SQL 去提速,優化。

     可以看到,ID=2 是FILTER,被FILTER的部分是 DM_GY_JG 和 它的主鍵 PK_DM_GY_JG,從執行計劃來看,ID=12 走的是索引唯一性掃描,然後回表,照理說,索引唯一性掃描只能獲取到1條rowid,然後回表取數,不會有效能問題,但是,具體問題要具體分析,如果FILTER的 驅動表 返回的資料量很大,也就意味著ID=11和ID=12 被掃描的次數將會特別多,還要考慮索引和表的體積,因為INDEX UNIQUE SCAN    是索引單塊讀,一次只能讀取一個索引塊,那麼那麼這樣效率就很低了。

     首先檢查FILTER 驅動表將會返回的結果集數:

select count(distinct st.fk_create_id)
  from t_ksgl_st st
  left join (select bbst.fk_st_id, bb.c_name, bb.c_type
               from T_KSGL_TKBBST bbst
               join t_ksgl_tkbb bb
                 on bb.c_id = bbst.fk_tkbb_id) tmp
    on st.c_id = tmp.fk_st_id
  left join dm_gy_jg jg
    on st.fk_dept_id = jg.id
 where st.c_deleted = 'N'

  ------結果 88

   也就是說將88個值給FILTER的被驅動表,也就是說 ID=11和ID=12 將會被執行88次單塊讀。

  再看錶和索引的體積:

select segment_name,bytes/1024/1024 from user_segments where segment_name in ('DM_GY_JG','PK_DM_GY_JG

DM_GY_JG   ---  17M
PK_DM_GY_JG --- 3M

     單個主鍵的索引體積已經到了3M,如果這88個值離散度比較高,那麼就通過該索引回表會引起很大的效能問題。

     下面再來看引起FILTER 的 where 條件中的OR EXISTS 部分:

                   and ((st.c_publish = '1' and exists
                        (select *
                            from (select jg.id
                                    from dm_gy_jg jg
                                   where (jg.dw_id = '7f304de353c358egcfg267f023db8f25' and jg.jgsx = '1')
                                      or jg.id = '7f304de353c358egcfg267f023db8f25') t
                           where t.id = st.fk_dept_id)) or
                       (st.c_publish = '0' and st.fk_create_id = '80ec73e9e66e4153a70a209cd68c17c4'))

      整體結構就是:

and ((xxxx and exists) or (xxxx and xxxx))

     那麼就可以在or 中間拆分成兩個部分 然後做union all

    原主體部分為:

select ST.C_ID,
                       ST.C_CONTENT,
                       ST.C_CODE,
                       ST.C_KEY_WORD,
                       ST.C_PHRASE,
                       decode(tmp.c_type,
                              1,
                              '崗位勝任能力',
                              2,
                              '職業技能鑑定',
                              3,
                              '技能專家選聘',
                              4,
                              '專項考試',
                              5,
                              '專業技術資格',
                              '') as lb,
                       ST.C_PUBLISH,
                       ST.FK_CREATE_ID,
                       ST.C_CREATE_TIME,
                       ST.FK_DEPT_ID,
                       ST.C_STATUS,
                       ST.C_TYPE,
                       jg.jgmc,
                       tmp.c_name as bbname
                  from t_ksgl_st st
                  left join (select bbst.fk_st_id, bb.c_name, bb.c_type
                              from T_KSGL_TKBBST bbst
                              join t_ksgl_tkbb bb
                                on bb.c_id = bbst.fk_tkbb_id) tmp
                    on st.c_id = tmp.fk_st_id
                  left join dm_gy_jg jg
                    on st.fk_dept_id = jg.id
                 where st.c_deleted = 'N'
                   and ((st.c_publish = '1' and exists
                        (select *
                            from (select jg.id
                                    from dm_gy_jg jg
                                   where (jg.dw_id = '7f304de353c358egcfg267f023db8f25' and jg.jgsx = '1')
                                      or jg.id = '7f304de353c358egcfg267f023db8f25') t
                           where t.id = st.fk_dept_id)) or
                       (st.c_publish = '0' and st.fk_create_id = '80ec73e9e66e4153a70a209cd68c17c4'))

     拆分成UINION ALL 以後為:  

select ST.C_ID,
        ST.C_CONTENT,
        ST.C_CODE,
        ST.C_KEY_WORD,
        ST.C_PHRASE,
        decode(tmp.c_type,
               1,
               '崗位勝任能力',
               2,
               '職業技能鑑定',
               3,
               '技能專家選聘',
               4,
               '專項考試',
               5,
               '專業技術資格',
               '') as lb,
        ST.C_PUBLISH,
        ST.FK_CREATE_ID,
        ST.C_CREATE_TIME,
        ST.FK_DEPT_ID,
        ST.C_STATUS,
        ST.C_TYPE,
        jg.jgmc,
        tmp.c_name as bbname
   from t_ksgl_st st
   left join (select bbst.fk_st_id, bb.c_name, bb.c_type
                from T_KSGL_TKBBST bbst
                join t_ksgl_tkbb bb
                  on bb.c_id = bbst.fk_tkbb_id) tmp
     on st.c_id = tmp.fk_st_id
   left join dm_gy_jg jg
     on st.fk_dept_id = jg.id
  where st.c_deleted = 'N'
    and ((st.c_publish = '1' and exists
         (select *
             from (select jg.id
                     from dm_gy_jg jg
                    where (jg.dw_id = '7f304de353c358egcfg267f023db8f25' and
                          jg.jgsx = '1')
                       or jg.id = '7f304de353c358egcfg267f023db8f25') t
            where t.id = st.fk_dept_id)))
 union all (select ST.C_ID,
                   ST.C_CONTENT,
                   ST.C_CODE,
                   ST.C_KEY_WORD,
                   ST.C_PHRASE,
                   decode(tmp.c_type,
                          1,
                          '崗位勝任能力',
                          2,
                          '職業技能鑑定',
                          3,
                          '技能專家選聘',
                          4,
                          '專項考試',
                          5,
                          '專業技術資格',
                          '') as lb,
                   ST.C_PUBLISH,
                   ST.FK_CREATE_ID,
                   ST.C_CREATE_TIME,
                   ST.FK_DEPT_ID,
                   ST.C_STATUS,
                   ST.C_TYPE,
                   jg.jgmc,
                   tmp.c_name as bbname
              from t_ksgl_st st
              left join (select bbst.fk_st_id, bb.c_name, bb.c_type
                          from T_KSGL_TKBBST bbst
                          join t_ksgl_tkbb bb
                            on bb.c_id = bbst.fk_tkbb_id) tmp
                on st.c_id = tmp.fk_st_id
              left join dm_gy_jg jg
                on st.fk_dept_id = jg.id
             where st.c_deleted = 'N'
               and (st.c_publish = '0' and
                   st.fk_create_id = '80ec73e9e66e4153a70a209cd68c17c4'))

        完整的SQL 改為:

select C_ID,
       C_CONTENT,
       C_CODE,
       C_KEY_WORD,
       C_PHRASE,
       lb,
       C_PUBLISH,
       FK_CREATE_ID,
       C_CREATE_TIME,
       FK_DEPT_ID,
       C_STATUS,
       C_TYPE,
       jgmc,
       bbname
  from (select temp.*
          from (select ST.C_ID,
                       ST.C_CONTENT,
                       ST.C_CODE,
                       ST.C_KEY_WORD,
                       ST.C_PHRASE,
                       decode(tmp.c_type,
                              1,
                              '崗位勝任能力',
                              2,
                              '職業技能鑑定',
                              3,
                              '技能專家選聘',
                              4,
                              '專項考試',
                              5,
                              '專業技術資格',
                              '') as lb,
                       ST.C_PUBLISH,
                       ST.FK_CREATE_ID,
                       ST.C_CREATE_TIME,
                       ST.FK_DEPT_ID,
                       ST.C_STATUS,
                       ST.C_TYPE,
                       jg.jgmc,
                       tmp.c_name as bbname
                  from t_ksgl_st st
                  left join (select bbst.fk_st_id, bb.c_name, bb.c_type
                              from T_KSGL_TKBBST bbst
                              join t_ksgl_tkbb bb
                                on bb.c_id = bbst.fk_tkbb_id) tmp
                    on st.c_id = tmp.fk_st_id
                  left join dm_gy_jg jg
                    on st.fk_dept_id = jg.id
                 where st.c_deleted = 'N'
                   and ((st.c_publish = '1' and exists
                        (select *
                            from (select jg.id
                                    from dm_gy_jg jg
                                   where (jg.dw_id =
                                         '7f304de353c358egcfg267f023db8f25' and
                                         jg.jgsx = '1')
                                      or jg.id =
                                         '7f304de353c358egcfg267f023db8f25') t
                           where t.id = st.fk_dept_id)))
                union all (select ST.C_ID,
                                 ST.C_CONTENT,
                                 ST.C_CODE,
                                 ST.C_KEY_WORD,
                                 ST.C_PHRASE,
                                 decode(tmp.c_type,
                                        1,
                                        '崗位勝任能力',
                                        2,
                                        '職業技能鑑定',
                                        3,
                                        '技能專家選聘',
                                        4,
                                        '專項考試',
                                        5,
                                        '專業技術資格',
                                        '') as lb,
                                 ST.C_PUBLISH,
                                 ST.FK_CREATE_ID,
                                 ST.C_CREATE_TIME,
                                 ST.FK_DEPT_ID,
                                 ST.C_STATUS,
                                 ST.C_TYPE,
                                 jg.jgmc,
                                 tmp.c_name as bbname
                            from t_ksgl_st st
                            left join (select bbst.fk_st_id,
                                             bb.c_name,
                                             bb.c_type
                                        from T_KSGL_TKBBST bbst
                                        join t_ksgl_tkbb bb
                                          on bb.c_id = bbst.fk_tkbb_id) tmp
                              on st.c_id = tmp.fk_st_id
                            left join dm_gy_jg jg
                              on st.fk_dept_id = jg.id
                           where st.c_deleted = 'N'
                             and (st.c_publish = '0' and
                                 st.fk_create_id =
                                 '80ec73e9e66e4153a70a209cd68c17c4'))) temp)
 group by C_ID,
          C_CONTENT,
          C_CODE,
          C_KEY_WORD,
          C_PHRASE,
          lb,
          C_PUBLISH,
          FK_CREATE_ID,
          C_CREATE_TIME,
          FK_DEPT_ID,
          C_STATUS,
          C_TYPE,
          jgmc,
          bbname
 order by c_create_time desc

     修改後SQL的執行計劃為:

---------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |               |   152 |   192K| 26514   (2)| 00:05:19 |
|   1 |  SORT GROUP BY                          |               |   152 |   192K| 26514   (2)| 00:05:19 |
|   2 |   VIEW                                  |               |   152 |   192K| 26513   (2)| 00:05:19 |
|   3 |    UNION-ALL                            |               |       |       |            |          |
|*  4 |     HASH JOIN OUTER                     |               |    20 | 28280 |  5890   (2)| 00:01:11 |
|   5 |      NESTED LOOPS OUTER                 |               |    14 |  5418 |  1772   (1)| 00:00:22 |
|   6 |       NESTED LOOPS                      |               |    14 |  4788 |  1758   (1)| 00:00:22 |
|   7 |        SORT UNIQUE                      |               |    35 |  2380 |    14   (0)| 00:00:01 |
|*  8 |         TABLE ACCESS BY INDEX ROWID     | DM_GY_JG      |    35 |  2380 |    14   (0)| 00:00:01 |
|   9 |          BITMAP CONVERSION TO ROWIDS    |               |       |       |            |          |
|  10 |           BITMAP OR                     |               |       |       |            |          |
|  11 |            BITMAP CONVERSION FROM ROWIDS|               |       |       |            |          |
|* 12 |             INDEX RANGE SCAN            | DW_ID         |       |       |     3   (0)| 00:00:01 |
|  13 |            BITMAP CONVERSION FROM ROWIDS|               |       |       |            |          |
|* 14 |             INDEX RANGE SCAN            | PK_DM_GY_JG   |       |       |     1   (0)| 00:00:01 |
|* 15 |        TABLE ACCESS BY INDEX ROWID      | T_KSGL_ST     |    14 |  3836 |  1743   (1)| 00:00:21 |
|* 16 |         INDEX RANGE SCAN                | IN_ST_DEPT_ID | 23066 |       |   146   (1)| 00:00:02 |
|  17 |       TABLE ACCESS BY INDEX ROWID       | DM_GY_JG      |     1 |    45 |     1   (0)| 00:00:01 |
|* 18 |        INDEX UNIQUE SCAN                | PK_DM_GY_JG   |     1 |       |     0   (0)| 00:00:01 |
|  19 |      VIEW                               |               |  1328K|  1301M|  4102   (2)| 00:00:50 |
|* 20 |       HASH JOIN                         |               |  1328K|   163M|  4102   (2)| 00:00:50 |
|  21 |        TABLE ACCESS FULL                | T_KSGL_TKBB   |  3571 |   219K|    27   (0)| 00:00:01 |
|  22 |        INDEX FAST FULL SCAN             | IDX_YWS6      |  1330K|    83M|  4059   (1)| 00:00:49 |
|* 23 |     HASH JOIN OUTER                     |               |   132 |   173K| 20623   (1)| 00:04:08 |
|  24 |      NESTED LOOPS OUTER                 |               |    90 | 28710 | 16504   (1)| 00:03:19 |
|* 25 |       TABLE ACCESS FULL                 | T_KSGL_ST     |    90 | 24660 | 16414   (1)| 00:03:17 |
|  26 |       TABLE ACCESS BY INDEX ROWID       | DM_GY_JG      |     1 |    45 |     1   (0)| 00:00:01 |
|* 27 |        INDEX UNIQUE SCAN                | PK_DM_GY_JG   |     1 |       |     0   (0)| 00:00:01 |
|  28 |      VIEW                               |               |  1328K|  1301M|  4102   (2)| 00:00:50 |
|* 29 |       HASH JOIN                         |               |  1328K|   163M|  4102   (2)| 00:00:50 |
|  30 |        TABLE ACCESS FULL                | T_KSGL_TKBB   |  3571 |   219K|    27   (0)| 00:00:01 |
|  31 |        INDEX FAST FULL SCAN             | IDX_YWS6      |  1330K|    83M|  4059   (1)| 00:00:49 |
---------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   4 - access("ST"."C_ID"="TMP"."FK_ST_ID"(+))
   8 - filter("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25' AND "JG"."JGSX"='1' OR 
              "JG"."ID"='7f304de353c358egcfg267f023db8f25')
  12 - access("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25')
  14 - access("JG"."ID"='7f304de353c358egcfg267f023db8f25')
  15 - filter("ST"."C_PUBLISH"='1' AND "ST"."C_DELETED"='N')
  16 - access("JG"."ID"="ST"."FK_DEPT_ID")
  18 - access("ST"."FK_DEPT_ID"="JG"."ID"(+))
  20 - access("BB"."C_ID"="BBST"."FK_TKBB_ID")
  23 - access("ST"."C_ID"="TMP"."FK_ST_ID"(+))
  25 - filter("ST"."FK_CREATE_ID"='80ec73e9e66e4153a70a209cd68c17c4' AND "ST"."C_PUBLISH"='0' 
              AND "ST"."C_DELETED"='N')
  27 - access("ST"."FK_DEPT_ID"="JG"."ID"(+))
  29 - access("BB"."C_ID"="BBST"."FK_TKBB_ID")
   可以看到FILTER消失了。

  進一步調整:
       可以看到ID=16 ID=15 T_KSGL_ST 作為Nested loop的被驅動表 有回表再過濾,說明ID=16 使用的索引有問題

       建立合適的索引:

    create index idx_9 on T_KSGL_ST(FK_DEPT_ID,C_PUBLISH,C_DELETED);
select count(*) from T_KSGL_ST; --- 1000355 該表有100多萬條資料
看到ID=25 T_KSGL_ST 做了Nested loop的驅動表,且 有過濾條件,檢視過濾條件的選擇性:
select count(*) from T_KSGL_ST ST WHERE "ST"."FK_CREATE_ID"='80ec73e9e66e4153a70a209cd68c17c4' AND "ST"."C_PUBLISH"='0' AND "ST"."C_DELETED"='N';

---結果為3


應該建立相應的索引

create index idx_10 on T_KSGL_ST(FK_CREATE_ID,C_PUBLISH,C_DELETED);

再次執行新SQL的執行計劃:
-------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |             |   152 |   192K|  9576   (2)| 00:01:55 |
|   1 |  SORT GROUP BY                          |             |   152 |   192K|  9576   (2)| 00:01:55 |
|   2 |   VIEW                                  |             |   152 |   192K|  9575   (2)| 00:01:55 |
|   3 |    UNION-ALL                            |             |       |       |            |          |
|*  4 |     HASH JOIN OUTER                     |             |    20 | 28280 |  5355   (2)| 00:01:05 |
|   5 |      NESTED LOOPS OUTER                 |             |    14 |  5418 |  1237   (1)| 00:00:15 |
|   6 |       NESTED LOOPS                      |             |    14 |  4788 |  1223   (1)| 00:00:15 |
|   7 |        SORT UNIQUE                      |             |    35 |  2380 |    14   (0)| 00:00:01 |
|*  8 |         TABLE ACCESS BY INDEX ROWID     | DM_GY_JG    |    35 |  2380 |    14   (0)| 00:00:01 |
|   9 |          BITMAP CONVERSION TO ROWIDS    |             |       |       |            |          |
|  10 |           BITMAP OR                     |             |       |       |            |          |
|  11 |            BITMAP CONVERSION FROM ROWIDS|             |       |       |            |          |
|* 12 |             INDEX RANGE SCAN            | DW_ID       |       |       |     3   (0)| 00:00:01 |
|  13 |            BITMAP CONVERSION FROM ROWIDS|             |       |       |            |          |
|* 14 |             INDEX RANGE SCAN            | PK_DM_GY_JG |       |       |     1   (0)| 00:00:01 |
|  15 |        TABLE ACCESS BY INDEX ROWID      | T_KSGL_ST   |    14 |  3836 |  1208   (1)| 00:00:15 |
|* 16 |         INDEX RANGE SCAN                | IDX_9       | 17183 |       |    91   (2)| 00:00:02 |
|  17 |       TABLE ACCESS BY INDEX ROWID       | DM_GY_JG    |     1 |    45 |     1   (0)| 00:00:01 |
|* 18 |        INDEX UNIQUE SCAN                | PK_DM_GY_JG |     1 |       |     0   (0)| 00:00:01 |
|  19 |      VIEW                               |             |  1328K|  1301M|  4102   (2)| 00:00:50 |
|* 20 |       HASH JOIN                         |             |  1328K|   163M|  4102   (2)| 00:00:50 |
|  21 |        TABLE ACCESS FULL                | T_KSGL_TKBB |  3571 |   219K|    27   (0)| 00:00:01 |
|  22 |        INDEX FAST FULL SCAN             | IDX_YWS6    |  1330K|    83M|  4059   (1)| 00:00:49 |
|* 23 |     HASH JOIN OUTER                     |             |   132 |   173K|  4219   (2)| 00:00:51 |
|  24 |      NESTED LOOPS OUTER                 |             |    90 | 28710 |   100   (0)| 00:00:02 |
|  25 |       TABLE ACCESS BY INDEX ROWID       | T_KSGL_ST   |    90 | 24660 |    10   (0)| 00:00:01 |
|* 26 |        INDEX RANGE SCAN                 | IDX_10      |    96 |       |     3   (0)| 00:00:01 |
|  27 |       TABLE ACCESS BY INDEX ROWID       | DM_GY_JG    |     1 |    45 |     1   (0)| 00:00:01 |
|* 28 |        INDEX UNIQUE SCAN                | PK_DM_GY_JG |     1 |       |     0   (0)| 00:00:01 |
|  29 |      VIEW                               |             |  1328K|  1301M|  4102   (2)| 00:00:50 |
|* 30 |       HASH JOIN                         |             |  1328K|   163M|  4102   (2)| 00:00:50 |
|  31 |        TABLE ACCESS FULL                | T_KSGL_TKBB |  3571 |   219K|    27   (0)| 00:00:01 |
|  32 |        INDEX FAST FULL SCAN             | IDX_YWS6    |  1330K|    83M|  4059   (1)| 00:00:49 |
-------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   4 - access("ST"."C_ID"="TMP"."FK_ST_ID"(+))
   8 - filter("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25' AND "JG"."JGSX"='1' OR 
              "JG"."ID"='7f304de353c358egcfg267f023db8f25')
  12 - access("JG"."DW_ID"='7f304de353c358egcfg267f023db8f25')
  14 - access("JG"."ID"='7f304de353c358egcfg267f023db8f25')
  16 - access("JG"."ID"="ST"."FK_DEPT_ID" AND "ST"."C_PUBLISH"='1' AND "ST"."C_DELETED"='N')
  18 - access("ST"."FK_DEPT_ID"="JG"."ID"(+))
  20 - access("BB"."C_ID"="BBST"."FK_TKBB_ID")
  23 - access("ST"."C_ID"="TMP"."FK_ST_ID"(+))
  26 - access("ST"."FK_CREATE_ID"='80ec73e9e66e4153a70a209cd68c17c4' AND "ST"."C_PUBLISH"='0' 
              AND "ST"."C_DELETED"='N')
  28 - access("ST"."FK_DEPT_ID"="JG"."ID"(+))
  30 - access("BB"."C_ID"="BBST"."FK_TKBB_ID")


      優化的成果就是  程式第一頁顯示只要0.9秒  資料全部刷完 需要9秒。