在进行数据库调优时,代码的性能瓶颈至关重要,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 = ''AND A.RUNID = B.RUNIDAND A.RUNID = C.RUNIDAND B.UNIT_NUMBER = C.UNIT_NUMBER注意:每次DBMS_PROFILER.START_PROFILER的输入参数需要改变,否则便不能分别运行后的运行结果了。除此之外B.UNIT_OWNER =‘’中的约束值如果是在package里面需要是包名,如果是procedure则是procedure的名字。实在在不知道什么名字时可以在PLSQL_PROFILER_UNITS中查一下。此外,运行结果可以通过PLSQL Developer来观察,需要新建一个test,在profiler中可以查看它的运行结果,示例如下: