Oracle - 自动跟踪中的一致性获取与SQL TRACE中的一致性读取之间的区别

时间:2014-11-22 09:56:22

标签: oracle oracle11g trace

问题

Autotrace中的Consistent Gets与SQL Trace中的Consistent Read有什么区别?

背景

在Oracle演示HR模式上运行SQL并运行Autotrace和SQL Trace(底部)。我预计块读取的数量将是相同的,但是Autotrace Consistent Gets是21并且SQL Trace Consistent Read是15.我想知道是什么造成了差异。

表没有更新,跟踪表示没有缓存未命中,因此我认为SQL所需的块都在缓存中。

来自USER_INDEXES的表数据块对于EMPLOYESS和DEPARTMENTS都是5。行分别为107和27。

SELECT /*+USE_HASH(e d) */ e.employee_id, e.first_name||' '||e.last_name as full_name, d.department_name FROM employees e, departments d WHERE e.department_id=d.department_id;

环境

Windows x64上的Oracle 11G R2 人力资源演示架构。

自动跟踪

Execution Plan
----------------------------------------------------------
Plan hash value: 2052257371

----------------------------------------------------------------------------------
| Id  | Operation          | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |   106 |  4028 |     7  (15)| 00:00:01 |
|*  1 |  HASH JOIN         |             |   106 |  4028 |     7  (15)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| DEPARTMENTS |    27 |   432 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| EMPLOYEES   |   107 |  2354 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         21  consistent gets
          0  physical reads
          0  redo size
       4502  bytes sent via SQL*Net to client
        601  bytes received via SQL*Net from client
          9  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
        106  rows processed

  1  SELECT /*+USE_HASH(e d) */  e.employee_id, e.first_name||' '||e.last_name as full_name, d.department_name
  2  FROM
  3    employees e, departments d
  4  WHERE
  5    e.department_id=d.department_id
  6  --  AND d.department_name = 'IT'
  7*

SQL TRACE

    SELECT /*+USE_HASH(e d) */  e.employee_id, e.first_name||' '||e.last_name as full_name, d.department_name
    FROM
      employees e, departments d
    WHERE
      e.department_id=d.department_id

    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        2      0.00       0.00          0         15          0         106
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total        4      0.00       0.00          0         15          0         106

    Misses in library cache during parse: 0
    Optimizer mode: ALL_ROWS
    Parsing user id: 85  

    Rows     Row Source Operation
    -------  ---------------------------------------------------
        106  HASH JOIN  (cr=15 pr=0 pw=0 time=105 us cost=7 size=4028 card=106)
         27   TABLE ACCESS FULL DEPARTMENTS (cr=7 pr=0 pw=0 time=26 us cost=3 size=432 card=27)
        107   TABLE ACCESS FULL EMPLOYEES (cr=8 pr=0 pw=0 time=106 us cost=3 size=2354 card=107)

整个TKPROF输出     TKPROF:版本11.2.0.1.0 - 2014年11月22日星期六20:05:34发展

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Trace file: nr_ora_6352_HR.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing 
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

SQL ID: 909v6n80saka9
Plan Hash: 0
ALTER SESSION SET SQL_TRACE=true


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 85  
********************************************************************************

SELECT /*+USE_HASH(e d) */  e.employee_id, e.first_name||' '||e.last_name as full_name, d.department_name
FROM
  employees e, departments d
WHERE
  e.department_id=d.department_id

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0         15          0         106
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.00       0.00          0         15          0         106

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 85  

Rows     Row Source Operation
-------  ---------------------------------------------------
    106  HASH JOIN  (cr=15 pr=0 pw=0 time=105 us cost=7 size=4028 card=106)
     27   TABLE ACCESS FULL DEPARTMENTS (cr=7 pr=0 pw=0 time=26 us cost=3 size=432 card=27)
    107   TABLE ACCESS FULL EMPLOYEES (cr=8 pr=0 pw=0 time=106 us cost=3 size=2354 card=107)

********************************************************************************

SQL ID: at7597y1bruv1
Plan Hash: 0
ALTER SESSION SET SQL_TRACE=false


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           0

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 85  



********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        2      0.00       0.00          0          0          0           0
Execute      3      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0         15          0         106
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        7      0.00       0.00          0         15          0         106

Misses in library cache during parse: 1
Misses in library cache during execute: 1


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      0      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        0      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0

    3  user  SQL statements in session.
    0  internal SQL statements in session.
    3  SQL statements in session.
********************************************************************************
Trace file: nr_ora_6352_HR.trc
Trace file compatibility: 11.1.0.7
Sort options: default

       1  session in tracefile.
       3  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
       3  SQL statements in trace file.
       3  unique SQL statements in trace file.
      48  lines in trace file.
       0  elapsed seconds in trace file.

更新

之前的Autotrace是从SQL Plus Release 11.2.0.1.0执行的,而SQL Trace是从SQL Developer 4.0.1.14执行的。通过从相同的SQL Plus运行SQL跟踪,结果匹配...

TKPROF: Release 11.2.0.1.0 - Development on Sun Nov 23 11:22:13 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Trace file: nr_ora_2836_HR.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing 
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

SQL ID: 909v6n80saka9
Plan Hash: 0
ALTER SESSION SET SQL_TRACE=true


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 85  
********************************************************************************

SELECT /*+USE_HASH(e d) */  e.employee_id, e.first_name||' '||e.last_name as full_name, d.department_name
FROM
  employees e, departments d
WHERE
  e.department_id=d.department_id

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        9      0.00       0.00          0         21          0         106
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       11      0.00       0.00          0         21          0         106

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 85  

Rows     Row Source Operation
-------  ---------------------------------------------------
    106  HASH JOIN  (cr=21 pr=0 pw=0 time=0 us cost=7 size=4028 card=106)
     27   TABLE ACCESS FULL DEPARTMENTS (cr=7 pr=0 pw=0 time=26 us cost=3 size=432 card=27)
    107   TABLE ACCESS FULL EMPLOYEES (cr=14 pr=0 pw=0 time=0 us cost=3 size=2354 card=107)

********************************************************************************

SQL ID: at7597y1bruv1
Plan Hash: 0
ALTER SESSION SET SQL_TRACE=false


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           0

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 85  



********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        2      0.00       0.00          0          0          0           0
Execute      3      0.00       0.00          0          0          0           0
Fetch        9      0.00       0.00          0         21          0         106
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       14      0.00       0.00          0         21          0         106

Misses in library cache during parse: 1
Misses in library cache during execute: 1


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      0      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        0      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0

    3  user  SQL statements in session.
    0  internal SQL statements in session.
    3  SQL statements in session.
********************************************************************************
Trace file: nr_ora_2836_HR.trc
Trace file compatibility: 11.1.0.7
Sort options: default

       1  session in tracefile.
       3  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
       3  SQL statements in trace file.
       3  unique SQL statements in trace file.
      55  lines in trace file.
       0  elapsed seconds in trace file.

1 个答案:

答案 0 :(得分:3)

不同之处在于tkprof输出显示了您的单个SELECT语句,但是autotrace输出的Statistics部分总结了2个语句,SELECT和1个递归调用,如该部分的第一行所示。

递归调用的一些示例是:触发器(INSERT,UPDATE,DELETE),动态采样(如果缺少表统计信息)或内部语句在第一次执行时查询数据字典中的数据。

尝试使用autotrace至少运行两次语句,并检查连续执行时此1次递归调用是否仍然存在。如果是,则SQL跟踪应包含SELECT语句和递归SQL。您应该将它们的总和与autotrace输出进行比较。 Tkprof报告跟踪文件中找到的SQL语句的递归级别以及总摘要。