移柱换梁-使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值