Oracle 11GR2 TKPROF - 如何捕获递归 call-tree

Oracle 11GR2 TKPROF - How to capture the recursive call-tree

Objective

找到一种在 TKPROF 输出中直观地捕获递归调用树的方法(指定 SYS=YES)。

环境

Oracle 数据库 11g 企业版发布 11.2.0.1.0 - 64 位生产
Windows 7 64 位 DELL Latitude core i7 2.8GHz 8G 内存和 SSD HDD

背景

我正在尝试理解问题 中的索引效果和性能指示。

要进一步了解索引幕后发生的情况,运行 SQL 跟踪创建索引语句。

执行

运行 跟踪索引创建(SQL 朝向底部)和 运行 tkprof with "sys=yes" 选项。

SQL> ALTER TABLE TBL2 ADD CONSTRAINT PK_TBL2_COL1 PRIMARY KEY(COL1) ;
Table altered.
Elapsed: 00:00:01.75

> trcsess clientid="CREATE_INDEX" output="report_createindex.trc" *.trc
> tkprof createindex.trc output=createindex.txt sys=yes

问题

我想知道是否有一种方法可以直观地捕获调用层次结构,例如使用 tkprof 或其他工具从跟踪 (.trc) 文件中解释计划。

生成的报告包括递归调用,例如 "ALTER TABLE TBL2 ADD" 诱导 "INDEX BUILD UNIQUE",并且可能还有进一步的系统递归调用。我想输出没有反映 call-hierarchy(首先是 parent,紧随其后的是孩子)。

TKPROF 输出

SQL ID: 2w9c2khpsfj4m
Plan Hash: 3219312727
CREATE UNIQUE INDEX "TRY"."PK_TBL2_COL1" on "TRY"."TBL2"("COL1") NOPARALLEL


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          1          0           0
Execute      1      0.63       0.84       2999      15565       3173           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.63       0.85       2999      15566       3173           0

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 106     (recursive depth: 1)  <----------------- child?

Rows     Row Source Operation
-------  ---------------------------------------------------
      1  INDEX BUILD UNIQUE PK_TBL2_COL1 (cr=15904 pr=3003 pw=2090 time=0 us)(object id 0)
1000000   SORT CREATE INDEX (cr=15486 pr=2997 pw=0 time=208370 us)
1000000    TABLE ACCESS FULL TBL2 (cr=15486 pr=2997 pw=0 time=245360 us cost=4413 size=5000000 card=1000000)


Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  db file sequential read                       575        0.00          0.08
  db file scattered read                        138        0.00          0.07
  direct path write                               1        0.00          0.00
********************************************************************************

SQL ID: 47f85g3cmftry
Plan Hash: 0
ALTER TABLE TBL2 ADD CONSTRAINT PK_TBL2_COL1 PRIMARY KEY(COL1) 

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.20       0.64      15630      29477          3           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.20       0.64      15630      29477          3           0

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 106  <------------------------------------------ parent?

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  db file sequential read                         4        0.00          0.00
  db file scattered read                        259        0.01          0.42
  db file parallel read                           2        0.00          0.00
  SQL*Net message to client                       1        0.00          0.00
  SQL*Net message from client                     1        0.00          0.00
********************************************************************************

SQL

CREATE TABLE TBL2 (
    "COL1" NUMBER,
    "COL2" VARCHAR2(100 BYTE)
    -- CONSTRAINT "PK_TBL2_COL1" PRIMARY KEY ("COL1")
);
INSERT INTO TBL2 /*+ APPEND MONITOR GATHER_PLAN_STATISTICS CONNECTBY_INSERT */
SELECT LEVEL, rpad(TO_CHAR(LEVEL),100,'A') FROM DUAL CONNECT BY LEVEL <= 1000000
COMMIT;    
---------------------------------------------------------------------------
-- Flush the buffers and clear the caches.
---------------------------------------------------------------------------
ALTER SYSTEM CHECKPOINT;
ALTER SYSTEM FLUSH SHARED_POOL;
ALTER SYSTEM FLUSH BUFFER_CACHE;
ALTER SYSTEM SWITCH LOGFILE;
---------------------------------------------------------------------------
-- Start monitoring
---------------------------------------------------------------------------
ALTER SESSION SET TRACEFILE_IDENTIFIER ='CREATE_INDEX';
ALTER SESSION SET TIMED_STATISTICS=true;
BEGIN
  DBMS_SESSION.SET_IDENTIFIER('CREATE_INDEX');
  DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE('CREATE_INDEX', waits=>true, binds=>false);
  DBMS_MONITOR.CLIENT_ID_STAT_ENABLE('CREATE_INDEX');
END;
/
---------------------------------------------------------------------------
-- Run the SQL to insert and monitor
---------------------------------------------------------------------------
SET PAGESIZE 200 
SET LINESIZE 200
SET TIMING ON
SET ECHO ON
SET AUTOTRACE ON

ALTER TABLE TBL2 ADD CONSTRAINT PK_TBL2_COL1 PRIMARY KEY(COL1) ;

SET AUTOTRACE OFF
SET ECHO OFF
SET TIMING OFF
---------------------------------------------------------------------------
-- Stop monitoring.
---------------------------------------------------------------------------
BEGIN
  DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE('CREATE_INDEX');
  DBMS_MONITOR.CLIENT_ID_STAT_DISABLE('CREATE_INDEX');
END;
/

参考资料

Sometimes, to execute a SQL statement issued by a user, Oracle Database must issue additional statements. ...

If recursive calls occur while the SQL Trace facility is enabled, then TKPROF produces statistics for the recursive SQL statements and marks them clearly as recursive SQL statements in the output file.

我推荐traceanalyzer。您可以从 oracle 支持下载它。 TRCANLZR (TRCA):SQL_TRACE/Event 10046 跟踪文件分析器 - 用于解释原始 SQL 跟踪的工具(文档 ID 224270.1)

跟踪分析器有一个章节 "SQL Genealogy",其中显示了树状视图的递归 SQL。

安装跟踪分析器后,您可以使用

调用它
@trcanlzr <name of tracefile>

您甚至可以从客户端远程执行此操作(将 trcanlzr.sql 脚本复制到本地 SQLPATH)。它最终会将包含分析的 html 文件复制到您的客户端计算机。

根据@Jan 提供的信息,运行 TRCA。我理解 SQL Genealogy 是调用层次结构和细分,但请提供任何 suggestions/corrections。

SQL

SET AUTOTRACE OFF
SET ECHO ON
SET TIMING OFF

DROP TABLE TBL2 PURGE;
CREATE TABLE TBL2 (
    "COL1" NUMBER,
    "COL2" VARCHAR2(100 BYTE)
    -- CONSTRAINT "PK_TBL2_COL1" PRIMARY KEY ("COL1")
);
INSERT INTO TBL2 /*+ APPEND MONITOR GATHER_PLAN_STATISTICS CONNECTBY_INSERT */
SELECT LEVEL, rpad(TO_CHAR(LEVEL),100,'A') FROM DUAL CONNECT BY LEVEL <= 1000000;
COMMIT;    
---------------------------------------------------------------------------
-- Start monitoring
---------------------------------------------------------------------------
ALTER SESSION SET TRACEFILE_IDENTIFIER ='CREATE_INDEX';
ALTER SESSION SET TIMED_STATISTICS=TRUE;
alter session set events '10046 trace name context forever, level 8';

---------------------------------------------------------------------------
-- Run the SQL to insert and monitor
---------------------------------------------------------------------------
ALTER TABLE TBL2 ADD CONSTRAINT PK_TBL2_COL1 PRIMARY KEY(COL1) ;

---------------------------------------------------------------------------
-- Stop monitoring.
---------------------------------------------------------------------------
alter session set events '10046 trace name context off'; 

TRCA

@run/trcanlzr.sql nr_ora_6976_CREATE_INDEX.trc

顶部SQL (CPU)

SQL家谱

我使用 orasrp 来分析跟踪文件。它以易于阅读的形式生成带有层次结构计划的 html 报告。 它也易于使用(在控制台中)-

orasrp in_trace.trc out_report.html