包需要返回执行计划和查询统计信息

时间:2012-05-31 07:01:17

标签: oracle plsql oracle11gr2

假设我在sql plus中执行我的脚本,如下所示

sql>set autotrace traceonly;
sql>select * from employees;


Execution Plan
----------------------------------------------------------
   0       SELECT STATEMENT Optimizer Mode=ALL_ROWS (Cost=2 Card=14 Bytes=1 K)
   1    0    TABLE ACCESS FULL T416493.EMPLOYEE (Cost=2 Card=14 Bytes=1 K)

Statistics
----------------------------------------------------------
      6  user calls
      0  physical read total multi block requests
      0  physical read total bytes
      0  cell physical IO interconnect bytes
      0  commit cleanout failures: block lost
      0  IMU commits
      0  IMU Flushes
      0  IMU contention
      0  IMU bind flushes
      0  IMU mbu flush
     14  rows processed

我正在创建一个包,我在其中传递查询,它将为我提供此统计信息的执行计划。如何实现这是包?

1 个答案:

答案 0 :(得分:1)

这是DMBS_XPLAN的基本包装。

CREATE OR REPLACE PROCEDURE print_plan(p_sql IN VARCHAR2)
IS
BEGIN
  EXECUTE IMMEDIATE 'explain plan for '||p_sql;

  FOR r_plan IN (SELECT * 
                 FROM table(DBMS_XPLAN.DISPLAY))
  LOOP
    dbms_output.put_line(r_plan.plan_table_output);
  END LOOP;
END;
/

BEGIN
  print_plan('select 1 from dual');
END;
/