在数据库调优的时候,发现代码的性能瓶颈至关重要,oracle提供的DBMS_PROFILER包可以非常方便的发现其瓶颈所在。
DBMS_PROFILER在使用之前可能需要安装,安装方式如下:
1:执行$ORACLE_HOME/rdbms/admin/profload.sql
2:创建一个用于存放跟踪信息的schema
CREATE USER profiler IDENTIFIED BY profiler DEFAULT TABLESPACE users QUOTA UNLIMITED ON users;
GRANT connect TO profiler;
CREATE PUBLIC SYNONYM plsql_profiler_runs FOR profiler.plsql_profiler_runs;
CREATE PUBLIC SYNONYM plsql_profiler_units FOR profiler.plsql_profiler_units;
CREATE PUBLIC SYNONYM plsql_profiler_data FOR profiler.plsql_profiler_data;
CREATE PUBLIC SYNONYM plsql_profiler_runnumber FOR profiler.plsql_profiler_runnumber;
3:使用刚刚创建的profiler用户执行如下脚本或代码:
$ORACLE_HOME/rdbms/admin/proftab.sql
GRANT SELECT ON plsql_profiler_runnumber TO PUBLIC;
GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_data TO PUBLIC;
GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_units TO PUBLIC;
GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_runs TO PUBLIC;
安装成功后测试:
执行
BEGIN
DBMS_PROFILER.START_PROFILER('123');
FOR I IN 1 .. 1000 LOOP
INSERT INTO A VALUES (I || '');
END LOOP;
COMMIT;
DBMS_PROFILER.STOP_PROFILER();
END;
执行后使用下面的代码检查:
SELECT C.LINE#, C.TOTAL_OCCUR, C.TOTAL_TIME, C.MIN_TIME, C.MAX_TIME
FROM PLSQL_PROFILER_RUNS A, PLSQL_PROFILER_UNITS B, PLSQL_PROFILER_DATA C
WHERE A.RUN_COMMENT = '123'
AND B.UNIT_OWNER = '<ANONYMOUS>'
AND A.RUNID = B.RUNID
AND A.RUNID = C.RUNID
AND B.UNIT_NUMBER = C.UNIT_NUMBER
注意:每次DBMS_PROFILER.START_PROFILER的输入参数需要改变,否则便不能分别运行后的运行结果了。
另外B.UNIT_OWNER =‘’中的约束值如果是在package里面需要是包名,如果是procedure则是procedure的名字。是在不知道是什么名字就在PLSQL_PROFILER_UNITS差一下吧。
來源:http://hi.baidu.com/fly_ch/blog/item/cd06331c3fdf3e8f86d6b6d2.html