移柱換梁-使用outlines優化前端語言內嵌SQL


1 創建兩個outline
   bad_outline  是當前垃圾執行計劃
   good_outline 是加入提示後正確的執行計劃
SQL> create outline bad_outline  on select * from emp e, dept d where e.deptno=d.deptno ;

Outline created.

SQL>  create outline good_outline  on select /*+ use_merge(e ,d)  */* from emp e, dept d where e.deptno=d.deptno ;

Outline created.

2 開啓outline


SQL>  alter system set use_stored_outlines =true; 

System altered.


SQL> alter outline bad_outline enable ;

Outline altered.

SQL> alter outline good_outline enable ;

Outline altered.


3 查看當前outline的使用
SQL> set autotrace trace 
SQL>  select * from emp e, dept d where e.deptno=d.deptno ;

14 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 615168685

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    14 |   812 |     7  (15)| 00:00:01 |
|*  1 |  HASH JOIN         |      |    14 |   812 |     7  (15)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| DEPT |     4 |    80 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| EMP  |    14 |   532 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPTNO"="D"."DEPTNO")

Note
-----
   - outline "BAD_OUTLINE" used for this statement  --outline 已經被使


Statistics
----------------------------------------------------------
         39  recursive calls
        219  db block gets
         32  consistent gets
          0  physical reads
        632  redo size
       2015  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
         14  rows processed

SQL> 
SQL>  select /*+ use_merge(e ,d)  */* from emp e, dept d where e.deptno=d.deptno ;


14 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1407029907

----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    14 |   812 |     8  (25)| 00:00:01 |
|   1 |  MERGE JOIN         |      |    14 |   812 |     8  (25)| 00:00:01 |
|   2 |   SORT JOIN         |      |     4 |    80 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| DEPT |     4 |    80 |     3   (0)| 00:00:01 |
|*  4 |   SORT JOIN         |      |    14 |   532 |     4  (25)| 00:00:01 |
|   5 |    TABLE ACCESS FULL| EMP  |    14 |   532 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("E"."DEPTNO"="D"."DEPTNO")
       filter("E"."DEPTNO"="D"."DEPTNO")

Note
-----
   - outline "GOOD_OUTLINE" used for this statement  --outline 已經被使用


Statistics
----------------------------------------------------------
         37  recursive calls
        219  db block gets
         31  consistent gets
          0  physical reads
        584  redo size
       1843  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
         14  rows processed


4 修改基表 將outline 內容對調
SQL> UPDATE OUTLN.OL$HINTS
  2  SET OL_NAME=DECODE(OL_NAME, 'BAD_OUTLINE', 'GOOD_OUTLINE', 'GOOD_OUTLINE', 'BAD_OUTLINE')
  3  WHERE OL_NAME IN ('BAD_OUTLINE','GOOD_OUTLINE');

18 rows updated.

SQL> update outln.ol$ ol1
  2  set hintcount = (
  3          select  count(*) 
  4          from    outln.ol$hints ol2
  5          where   ol2.ol_name = ol1.ol_name
  6          )
  7  where
  8     ol1.ol_name in ('BAD_OUTLINE','GOOD_OUTLINE');

2 rows updated.

SQL> commit ;


5 修改後重新啓用outline

注意未加提示的sql語句執行計劃已經與加入提示use_merge的執行計劃相同
SQL> SQL> alter outline bad_outline enable ;

Outline altered.

SQL> select * from emp e, dept d where e.deptno=d.deptno ;


14 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1407029907

----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    14 |   812 |     8  (25)| 00:00:01 |
|   1 |  MERGE JOIN         |      |    14 |   812 |     8  (25)| 00:00:01 |
|   2 |   SORT JOIN         |      |     4 |    80 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| DEPT |     4 |    80 |     3   (0)| 00:00:01 |
|*  4 |   SORT JOIN         |      |    14 |   532 |     4  (25)| 00:00:01 |
|   5 |    TABLE ACCESS FULL| EMP  |    14 |   532 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("E"."DEPTNO"="D"."DEPTNO")
       filter("E"."DEPTNO"="D"."DEPTNO")

Note
-----
   - outline "BAD_OUTLINE" used for this statement


Statistics
----------------------------------------------------------
         31  recursive calls
        216  db block gets
         30  consistent gets
          0  physical reads
          0  redo size
       1843  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
         14  rows processed

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章