为快捷显示Oracle执行计划创建存储过程

为快捷显示Oracle执行计划创建存储过程


第一种:不设置输出格式参数,即用默认的

SQL> create or replace procedure sql_explain(v_sql varchar2)
  2  is
  3  type explain_cursor_type is ref cursor;
  4  explain_cursor explain_cursor_type;
  5  a varchar2(2048);
  6  begin
  7    execute immediate 'explain plan for '||v_sql;
  8    open explain_cursor for select PLAN_TABLE_OUTPUT  from table(dbms_xplan.display());
  9    loop
 10      fetch explain_cursor into a;
 11      exit when explain_cursor%NOTFOUND;
 12     dbms_output.put_line(a);
 13    end loop;
 14  end;
 15  /

Procedure created.
SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=1');
Plan hash value: 2680223496
--------------------------------------------------------------------------------------
| Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1)
4 - filter("B"."ID"=1)

PL/SQL procedure successfully completed.

SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=''1''');
Plan hash value: 2680223496
--------------------------------------------------------------------------------------
| Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1)
4 - filter("B"."ID"=1)

PL/SQL procedure successfully completed.

第二种:添加format参数,灵活选择

SQL> create or replace procedure sql_explain(v_sql varchar2,v_format varchar2)
  2  is
  3  type explain_cursor_type is ref cursor;
  4  explain_cursor explain_cursor_type;
  5  a varchar2(2048);
  6  begin
  7    execute immediate 'explain plan for '||v_sql;
  8    open explain_cursor for select PLAN_TABLE_OUTPUT  from table(dbms_xplan.display(null,null,v_format));
  9    loop
 10      fetch explain_cursor into a;
 11      exit when explain_cursor%NOTFOUND;
 12     dbms_output.put_line(a);
 13    end loop;
 14  end;
 15  /

Procedure created.
SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=1','all');
Plan hash value: 2680223496
--------------------------------------------------------------------------------------
| Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / A@SEL$1
3 - SEL$1 / A@SEL$1
4 - SEL$1 / B@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1)
4 - filter("B"."ID"=1)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) "A"."NAME"[VARCHAR2,32], "B"."NAME"[VARCHAR2,32]
2 - "A"."NAME"[VARCHAR2,32]
3 - "A".ROWID[ROWID,10]
4 - "B"."NAME"[VARCHAR2,32]

PL/SQL procedure successfully completed.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值