《Oracle PL/SQL例項精講》學習筆記26——優化PL/SQL(第一部分——PL/SQL調優工具)
阿新 • • 發佈:2018-11-22
本章內容:
1. PL/SQL調優工具(使用PL/SQL剖析器API、使用跟蹤API、使用PL/SQL層次式剖析器)
2. PL/SQL優化級別
3. 子程式內聯
程式碼如下:
1. 檢視PLSQL_DEBUG引數
SQL> show parameter plsql_debug NAME TYPE VALUE ------------------------------------ --------------------------------- ------------------------------ plsql_debug boolean FALSE
2. 啟用會話級PLSQL_DEBUG
SQL> ALTER SESSION SET PLSQL_DEBUG = TRUE;
Session altered.
3. 執行TRACETABL.SQL
SQL> @D:\app\<USER>\product\12.1.0\dbhome_2\RDBMS\ADMIN\tracetab.sql Table dropped. Table dropped. Sequence dropped. Table created. Comment created. Table created. Comment created. Sequence created.
其中,TRACETABL.SQL的內容如下:
Rem Rem $Header: tracetab.sql 31-mar-2006.16:45:38 jmuller Exp $ Rem Rem tracetab.sql Rem Rem Copyright (c) 1999, 2006, Oracle. All rights reserved. Rem Rem NAME Rem tracetab.sql Rem Rem DESCRIPTION Rem Create tables for the PL/SQL tracer Rem Rem NOTES Rem This script must be run under SYS Rem Rem The following tables are required to collect data Rem plsql_trace_runs - information on trace runs Rem plsql_trace_event - detailed trace data Rem Rem For security reasons, these tables are created under SYS, and are Rem unique system-wide. The DBA should explicitly grant access to these Rem tables to those users who require it. Rem Rem The plsql_trace_runnumber sequence is used for generating unique Rem run numbers. Rem Rem THIS SCRIPT DELETES ALL EXISTING DATA! Rem Rem MODIFIED (MM/DD/YY) Rem jmuller 03/14/06 - Fix bug 5066528: several tracing enhancements Rem dalpern 05/18/05 - 4180912: improved info in PLSQL_TRACE_EVENTS Rem jmuller 10/16/02 - Fix bug 2610154: widen *dblink columns Rem astocks 09/16/99 - Make use of sys consistent Rem jmuller 10/07/99 - Fix bug 708690: TAB -> blank Rem astocks 06/07/99 - Tables for PL/SQL tracer Rem astocks 06/07/99 - Created Rem Rem drop table sys.plsql_trace_events cascade constraints; drop table sys.plsql_trace_runs cascade constraints; drop sequence sys.plsql_trace_runnumber; create table sys.plsql_trace_runs ( runid number primary key, -- unique run identifier, -- from plsql_trace_runnumber run_date date, -- start time of run run_owner varchar2(31), -- account under which run was made run_comment varchar2(2047), -- user provided comment for this run run_comment1 varchar2(2047), -- additional user-supplied comment run_end date, -- termination time for this run run_flags varchar2(2047), -- flags for this run related_run number, -- for correlating client/server run_system_info varchar2(2047), -- currently unused spare1 varchar2(256) -- unused ); comment on table sys.plsql_trace_runs is 'Run-specific information for the PL/SQL trace'; create table sys.plsql_trace_events ( runid number references sys.plsql_trace_runs,-- run identifier event_seq number, -- unique sequence number within run event_time date, -- timestamp related_event number, event_kind number, event_unit_dblink varchar2(4000), event_unit_owner varchar2(31), event_unit varchar2(31), -- unit where the event happened event_unit_kind varchar2(31), event_line number, -- line in the unit where event happened event_proc_name varchar2(31), -- if not empty, procedure where event -- happened stack_depth number, -- -- Fields that apply to procedure calls proc_name varchar2(31), -- if not empty, name of procedure called proc_dblink varchar2(4000), proc_owner varchar2(31), proc_unit varchar2(31), proc_unit_kind varchar2(31), proc_line number, proc_params varchar2(2047), -- -- Fields that apply to ICDs (Calls to PL/SQL internal routines) icd_index number, -- -- Fields that apply to exceptions user_excp number, excp number, -- -- Field for comments -- User defined event - text supplied by user -- SQL event - actual SQL string -- Others - Description of event event_comment varchar2(2047), ---- -- Fields for bulk binds -- ? -- -- Fields from dbms_application_info, dbms_session, and ECID module varchar2(4000), action varchar2(4000), client_info varchar2(4000), client_id varchar2(4000), ecid_id varchar2(4000), ecid_seq number, -- -- -- Fields for extended callstack and errorstack info -- (currently set only for "Exception raised", "Exception handled" and "Trace -- flags changed" ([5066528]) events) -- callstack clob, errorstack clob, -- primary key(runid, event_seq) ); comment on table sys.plsql_trace_events is 'Accumulated data from all trace runs'; create sequence sys.plsql_trace_runnumber start with 1 nocache;
4. 使用者授權
SQL> grant select on plsql_trace_events to c##student;
Grant succeeded.
SQL> grant select on plsql_trace_runs to c##student;
Grant succeeded.
5. 測試
SQL> /* Formatted on 2018/11/19 23:11:50 (QP5 v5.256.13226.35538) */
SQL> CREATE OR REPLACE PROCEDURE TEST_TRACE
2 AS
3 v_num1 NUMBER;
4 v_num2 NUMBER;
5 v_num3 NUMBER;
6 v_date DATE;
7 BEGIN
8 FOR i IN 1 .. 10
9 LOOP
10 v_num1 := 1;
11 v_num2 := i + i / 2 + SQRT (i);
12 v_num3 := v_num1 + v_num2;
13
14 SELECT SYSDATE INTO v_date FROM DUAL;
15 END LOOP;
16 END;
17 /
Procedure created.
SQL>
SQL> BEGIN
2 DBMS_TRACE.SET_PLSQL_TRACE(DBMS_TRACE.TRACE_ALL_CALLS);
3 TEST_TRACE;
4 DBMS_TRACE.CLEAR_PLSQL_TRACE;
5 END;
6 /
SQL> SELECT R.RUNID,
2 E.EVENT_SEQ,
3 E.EVENT_UNIT_OWNER,
4 E.EVENT_UNIT,
5 E.EVENT_UNIT_KIND,
6 E.PROC_LINE,
7 E.EVENT_COMMENT
8 FROM PLSQL_TRACE_RUNS R, PLSQL_TRACE_EVENTS E
9 WHERE R.RUNID = 1 AND R.RUNID = E.RUNID
10 ORDER BY R.RUNID, E.EVENT_SEQ;
RUNID EVENT_SEQ EVENT_UNIT EVENT_UNIT EVENT_UNIT_KIND PROC_LINE EVENT_COMMENT
----- --------- ---------- ------------ --------------- --------- ------------------------------
1 1 PL/SQL Trace Tool started
1 2 Trace flags changed
1 3 SYS DBMS_TRACE PACKAGE BODY 75 Return from procedure call
1 4 SYS DBMS_TRACE PACKAGE BODY 81 Return from procedure call
1 5 SYS DBMS_TRACE PACKAGE BODY 3 Return from procedure call
1 6 <anonymous> ANONYMOUS BLOCK 1 Procedure Call
1 7 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 8 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 9 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 10 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 11 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 12 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 13 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 14 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 15 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 16 SYS TEST_TRACE PROCEDURE PL/SQL Internal Call
1 17 SYS TEST_TRACE PROCEDURE 4 Return from procedure call
1 18 <anonymous> ANONYMOUS BLOCK 92 Procedure Call
1 19 SYS DBMS_TRACE PACKAGE BODY 69 Procedure Call
1 20 SYS DBMS_TRACE PACKAGE BODY 64 Procedure Call
1 21 SYS DBMS_TRACE PACKAGE BODY 12 Procedure Call
1 22 SYS DBMS_TRACE PACKAGE BODY 66 Return from procedure call
1 23 SYS DBMS_TRACE PACKAGE BODY 72 Return from procedure call
1 24 SYS DBMS_TRACE PACKAGE BODY 21 Procedure Call
1 25 PL/SQL trace stopped