分析oracle的执行计划(explain plan)并对对sql进行优化实践

基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分析执行计划对sql进行优化做相应说明。

一、什么是执行计划(explain plan)

执行计划:一条查询语句在oracle中的执行过程或访问路径的描述。

二、如何查看执行计划

1.set autotrace on

2.explain plan for sql语句;

select plan_table_output from table(dbms_xplan.display());

3.通过第3方工具,如plsql developer(f5查看执行计划)、toad等;

三、看懂执行计划

1.执行计划中字段解释

  1. SQL> select * from scott.emp a,scott.emp b where a.empno=b.mgr;  
  2.  
  3. 已选择13行。  
  4.  
  5.    
  6.  
  7. 执行计划  
  8.  
  9. ----------------------------------------------------------  
  10.  
  11. Plan hash value: 992080948  
  12.  
  13. ---------------------------------------------------------------------------------------  
  14.  
  15. | Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  16.  
  17. ---------------------------------------------------------------------------------------  
  18.  
  19. |   0 | SELECT STATEMENT             |        |    13 |   988 |     6  (17)| 00:00:01 |  
  20.  
  21. |   1 |  MERGE JOIN                  |        |    13 |   988 |     6  (17)| 00:00:01 |  
  22.  
  23. |   2 |   TABLE ACCESS BY INDEX ROWID| EMP    |    14 |   532 |     2   (0)| 00:00:01 |  
  24.  
  25. |   3 |    INDEX FULL SCAN           | PK_EMP |    14 |       |     1   (0)| 00:00:01 |  
  26.  
  27. |*  4 |   SORT JOIN                  |        |    13 |   494 |     4  (25)| 00:00:01 |  
  28.  
  29. |*  5 |    TABLE ACCESS FULL         | EMP    |    13 |   494 |     3   (0)| 00:00:01 |  
  30.  
  31. ---------------------------------------------------------------------------------------  
  32.  
  33.    
  34.  
  35. Predicate Information (identified by operation id):  
  36.  
  37. ---------------------------------------------------  
  38.  
  39.    4 - access("A"."EMPNO"="B"."MGR")  
  40.  
  41.        filter("A"."EMPNO"="B"."MGR")  
  42.  
  43.    5 - filter("B"."MGR" IS NOT NULL)  
  44.  
  45.    
  46.  
  47. 统计信息  
  48.  
  49. ----------------------------------------------------------  
  50.  
  51.           0  recursive calls  
  52.  
  53.           0  db block gets  
  54.  
  55.          11  consistent gets  
  56.  
  57.           0  physical reads  
  58.  
  59.           0  redo size  
  60.  
  61.        2091  bytes sent via SQL*Net to client  
  62.  
  63.         416  bytes received via SQL*Net from client  
  64.  
  65.           2  SQL*Net roundtrips to/from client  
  66.  
  67.           1  sorts (memory)  
  68.  
  69.           0  sorts (disk)  
  70.  
  71.          13  rows processed  
  72.  
  73. SQL

对上面执行计划列字段的解释:

Id: 执行序列,但不是执行的先后顺序。执行的先后根据Operation缩进来判断(采用最右最上最先执行的原则看层次关系,在同一级如果某个动作没有子ID就最先执行。 一般按缩进长度来判断,缩进最大的最先执行,如果有2行缩进一样,那么就先执行上面的。)

如:上面执行计划的执行顺序为:3--》2--》5--》4--》1

Operation: 当前操作的内容。

Name:操作对象

Rows:也就是10g版本以前的Cardinality(基数),Oracle估计当前操作的返回结果集行数。

Bytes:表示执行该步骤后返回的字节数。

Cost(CPU):表示执行到该步骤的一个执行成本,用于说明SQL执行的代价。

Time:Oracle 估计当前操作的时间。

 

2.谓词说明:

Predicate Information (identified by operation id):

---------------------------------------------------

4 - access("A"."EMPNO"="B"."MGR")

filter("A"."EMPNO"="B"."MGR")

5 - filter("B"."MGR" IS NOT NULL)

 

Access: 表示这个谓词条件的值将会影响数据的访问路劲(全表扫描还是索引)。

Filter:表示谓词条件的值不会影响数据的访问路劲,只起过滤的作用。

在谓词中主要注意access,要考虑谓词的条件,使用的访问路径是否正确。

 

四、 动态分析

如果在执行计划中有如下提示:

Note

------------

-dynamic sampling used for the statement

 

这提示用户CBO当前使用的技术,需要用户在分析计划时考虑到这些因素。 当出现这个提示,说明当前表使用了动态采样。 我们从而推断这个表可能没有做过分析。

这里会出现两种情况:

(1) 如果表没有做过分析,那么CBO可以通过动态采样的方式来获取分析数据,也可以或者正确的执行计划。

(2) 如果表分析过,但是分析信息过旧,这时CBO就不会在使用动态采样,而是使用这些旧的分析数据,从而可能导致错误的执行计划。

 

五、表访问方式

1.Full Table Scan (FTS) 全表扫描

2.Index Lookup 索引扫描

There are 5 methods of index lookup:

index unique scan --索引唯一扫描

Method for looking up a single key value via a unique index. always returns a single value, You must supply AT LEAST the leading column of the index to access data via the index.

 

index range scan --索引局部扫描

Index range scan is a method for accessing a range values of a particular column. AT LEAST the leading column of the index must be supplied to access data via the index. Can be used for range operations (e.g. > < <> >= <= between) .

 

index full scan --索引全局扫描

Full index scans are only available in the CBO as otherwise we are unable to determine whether a full scan would be a good idea or not. We choose an index Full Scan when we have statistics that indicate that it is going to be more efficient than a Full table scan and a sort. For example we may do a Full index scan when we do an unbounded scan of an index and want the data to be ordered in the index order.

 

index fast full scan --索引快速全局扫描,不带order by情况下常发生

Scans all the block in the index, Rows are not returned in sorted order, Introduced in 7.3 and requires V733_PLANS_ENABLED=TRUE and CBO, may be hinted using INDEX_FFS hint, uses multiblock i/o, can be executed in parallel, can be used to access second column of concatenated indexes. This is because we are selecting all of the index.

 

index skip scan --索引跳跃扫描,where条件列是非索引的前导列情况下常发生

Index skip scan finds rows even if the column is not the leading column of a concatenated index. It skips the first column(s) during the search.

 

3.Rowid 物理ID扫描

This is the quickest access method available.Oracle retrieves the specified block and extracts the rows it is interested in. --Rowid扫描是最快的访问数据方式

 

六、表连接方式

请参照另一篇文章:Oracle 表连接方式详解

http://www.fengfly.com/plus/view-210420-1.html

 

七、运算符

1.sort --排序,很消耗资源

There are a number of different operations that promote sorts:

(1)order by clauses (2)group by (3)sort merge join –-这三个会产生排序运算

 

2.filter --过滤,如not in、min函数等容易产生

Has a number of different meanings, used to indicate partition elimination, may also indicate an actual filter step where one row source is filtering, another, functions such as min may introduce filter steps into query plans.

 

3.view --视图,大都由内联视图产生(可能深入到视图基表)

When a view cannot be merged into the main query you will often see a projection view operation. This indicates that the 'view' will be selected from directly as opposed to being broken down into joins on the base tables. A number of constructs make a view non mergeable. Inline views are also non mergeable.

 

4.partition view --分区视图

Partition views are a legacy technology that were superceded by the partitioning option. This section of the article is provided as reference for such legacy systems.

 

附:oracle优化器(Optimizer)

 

Oracle 数据库中优化器(Optimizer)是SQL分析和执行的优化工具,它负责指定SQL的执行计划,也就是它负责保证SQL执行的效率最高,比如优化器决定Oracle 以什么样的方式来访问数据,是全表扫描(Full Table Scan),索引范围扫描(Index Range Scan)还是全索引快速扫描(INDEX Fast Full Scan:INDEX_FFS);对于表关联查询,它负责确定表之间以一种什么方式来关联,比如HASH_JOHN还是NESTED LOOPS 或者MERGE JOIN。 这些因素直接决定SQL的执行效率,所以优化器是SQL 执行的核心,它做出的执行计划好坏,直接决定着SQL的执行效率。

Oracle 的优化器有两种:

RBO(Rule-Based Optimization): 基于规则的优化器

CBO(Cost-Based Optimization): 基于代价的优化器

从Oracle 10g开始,RBO 已经被弃用,但是我们依然可以通过Hint 方式来使用它。

 

在Oracle 10g中,CBO 可选的运行模式有2种:

(1) FIRST_ROWS(n)

Oracle 在执行SQL时,优先考虑将结果集中的前n条记录以最快的速度反馈回来,而其他的结果并不需要同时返回。

(2) ALL_ROWS -- 10g中的默认值

Oracle 会用最快的速度将SQL执行完毕,将结果集全部返回,它和FIRST_ROWS(n)的区别在于,ALL_ROWS强调以最快的速度将SQL执行完毕,并将所有的结果集反馈回来,而FIRST_ROWS(n)则侧重于返回前n条记录的执行时间。

修改CBO 模式的三种方法:

(1) SQL 语句:

Sessions级别:

SQL> alter session set optimizer_mode=all_rows;

(2) 修改pfile 参数:

OPTIMIZER_MODE=RULE/CHOOSE/FIRST_ROWS/ALL_ROWS

(3) 语句级别用Hint(/* + ... */)来设定

Select /*+ first_rows(10) */ name from table;

Select /*+ all_rows */ name from table;

 

相关:

 Oracle 执行计划(Explain Plan) 说明

使用 EXPLAIN PLAN 获取SQL语句执行计划

Oracle 执行计划(Explain Plan) 说明

Sql优化系列之(1)__where子句条件优化

 

TABLE ACCESS FULL  全表扫描

PARTITION RANGE ITERATOR 分区迭代扫描

partition range single单个分区扫描

posted @ 2016-09-22 19:18  jack_ou  阅读(3484)  评论(0编辑  收藏  举报