Oracle执行分析详细
我们以下面的一个例子来讲解
这里做个补充:trace的类型一共有以下几种
序号 |
命令 |
解释 |
1 |
SET AUTOTRACE OFF |
此为默认值,即关闭Autotrace |
2 |
SET AUTOTRACE ON EXPLAIN |
只显示执行计划 |
3 |
SET AUTOTRACE ON STATISTICS |
只显示执行的统计信息 |
4 |
SET AUTOTRACE ON |
包含2,3两项内容 |
5 |
SET AUTOTRACE TRACEONLY |
与ON相似,但不显示语句的执行结果 |
我喜欢SET AUTOTRACE TRACEONLY,我们以后的例子都是基于这种方式的
- SQL> select * from departments a where a.department_id in (select b.department_id from employees b where b.employee_id=205);
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 2782876085
- ----------------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- ----------------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 27 | 2 (0)| 00:00:01 |
- | 1 | NESTED LOOPS | | 1 | 27 | 2 (0)| 00:00:01 |
- | 2 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 7 | 1 (0)| 00:00:01 |
- |* 3 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0)| 00:00:01 |
- | 4 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 540 | 1 (0)| 00:00:01 |
- |* 5 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 |
- ----------------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 3 - access("B"."EMPLOYEE_ID"=205)
- 5 - access("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID")
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 4 consistent gets
- 0 physical reads
- 0 redo size
- 749 bytes sent via SQL*Net to client
- 492 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
SQL> select * from departments a where a.department_id in (select b.department_id from employees b where b.employee_id=205); Execution Plan ---------------------------------------------------------- Plan hash value: 2782876085 ---------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 27 | 2 (0)| 00:00:01 | | 1 | NESTED LOOPS | | 1 | 27 | 2 (0)| 00:00:01 | | 2 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 7 | 1 (0)| 00:00:01 | |* 3 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0)| 00:00:01 | | 4 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 540 | 1 (0)| 00:00:01 | |* 5 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 | ---------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("B"."EMPLOYEE_ID"=205) 5 - access("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID") Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 749 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed让我们来一行一行的看:
一、表部分
1、Plan hash value:
- Plan hash value: 2782876085
Plan hash value: 2782876085这一行是这一条语句的hash值,我们知道oracle对每条语句产生的执行计划放在share pool里面,第一次要经过硬解析,产生hash值。下次再执行该语句时候比较hash值,如果相同就不要执行硬解析。
2、Operation( 操作)
这里的东西就多了,就是把sql进行分解,让我一起看看上的sql,这段sql的第一步是employee_id=25,这里我们employee_id上面建了主键,建主键默认创建唯一索引。这里是用“=”进行限制的,所以走的unique scan方式。其他方式参考Oracle执行计划 讲解(一)内容
还有一个知识点,就是要知道表链接操作,见我的另外一篇文章()
3、Name(被操作的对象)
比如上例中的第二行operation(TABLE ACCESS BY INDEX ROWID)这里的TABLE对象为EMPLOYEES
4、Row,有的地方也叫Cardinality(用plsqldev里面解释计划窗口)
这里是数据查询的行数,比如说上个例子第4行, departments 这张表就要扫描27行,然后和子查询(select b.department_id from employees b where b.employee_id=205)的值进行比较。如果使用=(注:大部分时候是不能用=来替换,这里是特例)就不一样了。
- SQL> select * from departments a where a.department_id = (select b.department_id from employees b where b.employee_id=205);
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 3449260133
- -----------------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -----------------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 20 | 2 (0)| 00:00:01 |
- | 1 | TABLE ACCESS BY INDEX ROWID | DEPARTMENTS | 1 | 20 | 1 (0)| 00:00:01 |
- |* 2 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 |
- | 3 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 7 | 1 (0)| 00:00:01 |
- |* 4 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0)| 00:00:01 |
- -----------------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 2 - access("A"."DEPARTMENT_ID"= (SELECT /*+ */ "B"."DEPARTMENT_ID" FROM "EMPLOYEES"
- "B" WHERE "B"."EMPLOYEE_ID"=205))
- 4 - access("B"."EMPLOYEE_ID"=205)
- Statistics
- ----------------------------------------------------------
- 0 recursive calls
- 0 db block gets
- 4 consistent gets
- 0 physical reads
- 0 redo size
- 749 bytes sent via SQL*Net to client
- 492 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
SQL> select * from departments a where a.department_id = (select b.department_id from employees b where b.employee_id=205); Execution Plan ---------------------------------------------------------- Plan hash value: 3449260133 ----------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 20 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID | DEPARTMENTS | 1 | 20 | 1 (0)| 00:00:01 | |* 2 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 | | 3 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 7 | 1 (0)| 00:00:01 | |* 4 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0)| 00:00:01 | ----------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("A"."DEPARTMENT_ID"= (SELECT /*+ */ "B"."DEPARTMENT_ID" FROM "EMPLOYEES" "B" WHERE "B"."EMPLOYEE_ID"=205)) 4 - access("B"."EMPLOYEE_ID"=205) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 749 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
5、Byte
扫描的数据的字节数
6、Cost
这里上次讲过了,这里简单说下吧。
cost没有单位,是一个相对值,是sql文以cbo方式解析执行时,供oracle用来评估cbo成本,选择执行计划用的。
公式:Cost=(Single block I/O cost+ Multiblock I/O cost+ CPU cost)/sreadtim
没有明确的含义,不过对比时就非常有用了。
7、Time
每段执行的时间
二、Predicate Information
这里列出的是过滤条件,一共有两种:
1、索引(access)
如上例中的access("B"."EMPLOYEE_ID"=205),这里使用索引作为过滤条件
2、非索引(filter),看下面这里例子
- SQL> select employee_id
- from employees c
- where c.first_name = ‘Steven‘ 2 3 ;
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1445457117
- -------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 11 | 3 (0)| 00:00:01 |
- |* 1 | TABLE ACCESS FULL| EMPLOYEES | 1 | 11 | 3 (0)| 00:00:01 |
- -------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("C"."FIRST_NAME"=‘Steven‘)
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 8 consistent gets
- 0 physical reads
- 0 redo size
- 574 bytes sent via SQL*Net to client
- 492 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 2 rows processed
SQL> select employee_id from employees c where c.first_name = ‘Steven‘ 2 3 ; Execution Plan ---------------------------------------------------------- Plan hash value: 1445457117 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 11 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMPLOYEES | 1 | 11 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("C"."FIRST_NAME"=‘Steven‘) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 8 consistent gets 0 physical reads 0 redo size 574 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 2 rows processed
这里的过滤条件是FIRST_NAME,但是FIRST_NAME没有建立索引,这个时候使用filter,做标记。
三、Statistics(统计信息)
这里是重点要说的,如果看累了,可以去喝口水。O(∩_∩)O~
AUTOTRACE Statistics列解释
序号 |
列名 |
解释 |
1 |
recursive calls |
递归调查 |
2 |
db block gets |
从buffer cache中读取的block的数量 |
3 |
consistent gets |
从buffer cache中读取的undo数据的block的数量 |
4 |
physical reads |
从磁盘读取的block的数量 |
5 |
redo size |
DML生成的redo的大小 |
6 |
sorts (memory) |
在内存执行的排序量 |
7 |
sorts (disk) |
在磁盘上执行的排序量 |
1、recursive calls(重点讲解)
官网对recursive calls 的解释如下:
Recursive Calls: Number of recursive calls generated at both the user and system level.
Oracle Database maintains tables used for internal processing. When it needs to change these tables, Oracle Database generates an internal SQL statement, which in turn generates a recursive call.In short, recursive calls are
basically SQL performed on behalf of your SQL. So, if you had to parse the query, for example, you might have had to run some other queries to get data dictionary information. These would be recursive calls. Space management, security checks, calling
PL/SQL from SQL—all incur recursive SQL calls。
IBM上面也有一篇讲解,有兴趣可以看看
http://publib.boulder.ibm.com/tividd/td/ITMD/SC23-4724-00/en_US/HTML/oraclepac510rg59.htm
总结一下:
当执行一条SQL语句时,产生的对其他SQL语句的调用,这些额外的语句称之为‘‘recursive calls‘‘或‘‘recursive SQL statements‘‘.
在IBM 的那片文档里讲了触发Recursive Call的6种情况:
如:
(1)我们做一条insert 时,没有足够的空间来保存row记录,Oracle 通过Recursive Call 来动态的分配空间。
(2)执行DDL语句时,ORACLE总是隐含的发出一些recursive SQL语句,来修改数据字典信息,以便成功的执行该DDL语句。
(3)当Shared Pool过小,data dictionary cache 也会相应的过小,没有足够的空间存储ORACLE的系统数据字典信息时,会发生Recursive calls,这些Recursive calls会将数据字典信息从硬盘读入内存中。
(4)存储过程、触发器内如果有SQL调用的话,也会产生recursive SQL。
在这些情况中,主要是对数据字典的查询,通常发生在第一次执行时,第二次执行一般可显著降低。递归需要消耗大量的资源,如果操作复杂,很容易出现问题!
现在让我们举例说明:
- SQL> select * from employees;
- 107 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1445457117
- -------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 107 | 7276 | 3 (0)| 00:00:01 |
- | 1 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 |
- -------------------------------------------------------------------------------
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 15 consistent gets
- 0 physical reads
- 0 redo size
- 9997 bytes sent via SQL*Net to client
- 569 bytes received via SQL*Net from client
- 9 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 107 rows processed
SQL> select * from employees; 107 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1445457117 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 107 | 7276 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------- Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 15 consistent gets 0 physical reads 0 redo size 9997 bytes sent via SQL*Net to client 569 bytes received via SQL*Net from client 9 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 107 rows processed
让我们再执行一遍
- SQL> select * from employees;
- 107 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1445457117
- -------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- -------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 107 | 7276 | 3 (0)| 00:00:01 |
- | 1 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 |
- -------------------------------------------------------------------------------
- Statistics
- ----------------------------------------------------------
- 0 recursive calls
- 0 db block gets
- 15 consistent gets
- 0 physical reads
- 0 redo size
- 9997 bytes sent via SQL*Net to client
- 569 bytes received via SQL*Net from client
- 9 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 107 rows processed
SQL> select * from employees; 107 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1445457117 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 107 | 7276 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------- Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 15 consistent gets 0 physical reads 0 redo size 9997 bytes sent via SQL*Net to client 569 bytes received via SQL*Net from client 9 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 107 rows processed
在第一次查询employees时,产生了1次recursive Call,第二次查询的时候,因为数据字典的信息信息已经放在cache里,所以第二次的recursive call 为0. 如果第二次也没有完全cache,那么也是会产生recursive call,但次数比第一次少。
其他的从字面上面就可以看出来了,不需要多解释了吧。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。