oracle数据库优化-执行计划详细

转载地址:http://blog.itpub.net/30126024/viewspace-2141974/

执行计划__获取方法、查看执行顺序、统计信息详解

个人常用的获取执行计划的方法
1.select * from table(dbms_xplan.display_cursor('v$sql.sql_id',v$sql.CHILD_NUMBER,'advanced'));  --只有执行计划

3 select *from table(dbms_xplan.display_AWR('SQL_ID')  ----查看历史执行计划
2.set autotrace traceonly exp  --只有执行计划
1与2的区别在于当SQL有绑定变量时,1更准确(毕竟sql先执行再获取的执行计划),2可能不准确,其他情况下两者获取的执行计划基本一样
1 10046

alter session set events ‘10046 trace name context forever, level 8’;
alter session set tracefile_identifier='fan';
alter session set max_dump_file_size=unlimited;
alter session set events '10046 trace name context forever, level 8';
SQL>.............
alter session set events '10046 trace name context off';
tkprof  channel_ora_4917_fan.trc  hehe sys=no waits=yes
--------------------- 

原文:https://blog.csdn.net/ashic/article/details/52138845 
 

1. explain planfor获取(类似plsql中的f5)

Step1; explain plan for “sql”

Step2: select * from table(dbms_xplan.display)

 

2. set autot on

Step1:set autotrace on

Step2:执行sql,自然会有结果输出

类似的有:

Set autotrace on #(得到执行计划,输出运行结果)

Set autotrace traceonly #(得到执行计划,不输出运行结果)

Set autotrace traceonly explain #(得到执行计划,不输出运行结果和统计信息部分,仅展现执行计划部分)

Set autotrace traceonly statistics #(不输出运行结果和执行计划部分,仅展现统计信息部分)
--------------------- 
3. statistics_level=all

Step1:alter session setstatistics_level=all;

Step2:执行sql

Step3:select * fromtable(dbms_xplan.display_cursor(null,null,'allstats last'));
--------------------- 

原文:https://blog.csdn.net/gumengkai/article/details/60468811 
!
4. 通过dbms_xplan.display_cursor输入sql_id参数直接获取

Step1:select  * fromtable(dbms_xplan.display_cursor('&sq_id')); #从共享池获取

注:

a. select  * fromtable(dbms_xplan.display_awr('&sq_id')); 从awr性能视图获取

b. 查看多个sql的执行计划

select * from table(dbms_xplan.display_cursor('cyzznbykb509s',0));
    select * fromtable(dbms_xplan.display_cursor('cyzznbykb509s',1));
--------------------- 

原文:https://blog.csdn.net/gumengkai/article/details/60468811 

5. 10046 trace跟踪

Step1:alter session setevents '10046 trace name context forever,level 12'; (开启跟踪)

Step2:执行sql

Step3:alter session setevents '10046 trace name context off';   (关闭跟踪)

Step4:步骤4:找到跟踪后产生的文件

Step5:tkprof  trc文件  目标文件 sys=no sort=prsela,exeela,fchela  (格式化命令)
--------------------- 

原文:https://blog.csdn.net/gumengkai/article/details/60468811 
 

  1. 6. awrsqlrpt.sql

     

    Step1:@?/rdbms/admin/awrsqrpt.sql

    Step2:选择你要的断点(begin snap 和end snap)

    Step3:输入sql_id

  2. 二.适用场合分析


        1.如果某SQL执行非常长时间才会出结果,甚至慢到返回不了结果,这时候看执行计划就只能用方法1;
        2.跟踪某条SQL最简单的方法是方法1,其次就是方法2;
        3.如果想观察到某条SQL有多条执行计划的情况,只能用方法4和方法6;
        4.如果SQL中含有多函数,函数中套有SQL等多层递归调用,想准确分析,只能使用方法5;
        5.要想确保看到真实的执行计划,不能用方法1和方法2;
        6.要想获取表被访问的次数,只能使用方法3;
    原文:https://blog.csdn.net/gumengkai/article/details/60468811 
     


执行计划指标和统计信息指标值的总结:
1.sql执行多次不改变arraysize的情况下,每次的consistent gets不会变
2.sql执行一次改变一次arraysize的情况下,每次的consistent gets会变,但是v$sql_plan中记录的CPU_COST并没有改变,说明v$sql_plan.CPU_COST是CHILD_NUMBER对应的SQL在第一次执行时生成的
3.执行计划中的Cost (%CPU)等于v$sql_plan.COST而非v$sql_plan.CPU_COST
4.只是一个select(非select for update)时,db block gets是0
5.逻辑读(consistent gets+db block gets)和每批次处理的数据行的大小是有一定关系的。每批次处理的数据行越大,则逻辑读越小。所以减少逻辑读的一个方法就是增加arraysize。
6.执行计划中的Cost (%CPU)只和按物理相关,所以修改arraysize是没有办法减少物理读的,也就是Cost (%CPU)不变 




统计信息各个指标的解释可以在官方文档关于Statistics Descriptions中找到
recursive calls:Number of recursive calls generated at both the user and system level
db block gets:Number of times a CURRENT block was requested(DML产生的逻辑读).
consistent gets:Number of times a consistent read was requested for a block(select产生的逻辑读).
physical reads:Total number of data blocks read from disk. This number equals the value of "physical reads direct" plus all reads into buffer cache(物理读).
redo size:Total amount of redo generated in bytes
bytes sent via SQL*Net to client:Total number of bytes sent to the client from the foreground processes(可以理解为sql的查询结果的字节数)
bytes received via SQL*Net from client:Total number of bytes received from the client over Oracle Net Services 
SQL*Net roundtrips to/from client:Total number of Oracle Net Services messages sent to and received from the client(客户端和数据库服务器之间的交互次数,与arraysize(行预取数量)和rows processed相关,等于rows processed/arraysize)
sorts (memory):Number of sort operations that were performed completely in memory and did not require any disk writes
sorts (disk):Number of sort operations that required at least one disk write
rows processed:Total number of rows that the parsed SQL statement returns(可以理解为sql的查询结果的行数)



看执行计划的方法:
1、从上往下,第一个没有子节点的步骤先执行
2、对于兄弟节点,运用第一点,即靠上的节点先执行。
3、所有兄弟节点执行完以后,执行父节点。

执行顺序的原则是:由上至下找到第一个并列的两列开始,从上至下,从右向左
由上至下:在执行计划中一般含有多个节点,相同级别(或并列)的节点,靠上的优先执行,靠下的后执行
从右向左:在某个节点下还存在多个子节点,先从最靠右的子节点开始执行。

oracle数据库优化-执行计划详细_第1张图片
执行计划顺序为3、2、5、4、1、0

oracle数据库优化-执行计划详细_第2张图片
执行计划顺序为3、5、4、2、6、1、0

oracle数据库优化-执行计划详细_第3张图片
执行计划顺序为4、3、6、5、2、8、7、1、0

oracle数据库优化-执行计划详细_第4张图片
执行计划顺序为3、5、6、4、1、0




四种获取执行计划的方法
1.使用explain plan for和DBMS_XPLAN.DISPLAY
SQL> explain plan for select * from te123;
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

SQL> SELECT id,operation,options,object_name,position from plan_table

2.使用autotrace
set autotrace on
1、执行sql
2、显示sql的结果集
3、显示执行计划
4、显示sql执行后的统计信息
-------------------------
set autotrace traceonly
or
set autotrace traceonly exp stat 
1、执行sql
2、显示执行计划
3、显示sql执行后的统计信息
-------------------------
set autotrace traceonly exp | explain
1、显示执行计划(select不会执行sql即V$SQL.EXECUTIONS不会增加,但是insert、update、delete会执行SQL的即V$SQL.EXECUTIONS会增加)
-------------------------
set autotrace traceonly stat | statistics
1、执行sql
2、显示sql执行后的统计信息

3.使用dbms_xplan.display_cursor
select * from table(dbms_xplan.display_cursor('v$sql.sql_id',v$sql.CHILD_NUMBER,'advanced'));
官方文档对display_cursor这个函数的说明里面没有advanced这个参数值,只有BASIC、TYPICAL、ALL这几个,不过实践中发现advanced这个参数值显示的内容比这几个参数值显示的都多

4.使用v$sql_plan
v$sql_plan也可以来查询某个对象的SQL执行计划
SQL>select id,operation,options,object_name,object_owner from v$sql_plan where object_name='TABLE_NAME'





如下实验证明增加arraysize可以减少逻辑读(其中Elapsed时间到达一定阶段就不再减少了),但是物理读不会减少即Cost (%CPU)不变
SQL> set timing on
SQL> set arraysize 15
SQL> select * from t1;
1024000 rows selected.
Elapsed: 00:00:11.22
Execution Plan
----------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   512K|    41M|  1785   (1)| 00:00:22 |
|   1 |  TABLE ACCESS FULL| T1   |   512K|    41M|  1785   (1)| 00:00:22 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      93297  consistent gets
      12933  physical reads
          0  redo size
   96936855  bytes sent via SQL*Net to client
     751345  bytes received via SQL*Net from client
      68268  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
    1024000  rows processed

SQL> set arraysize 1500
SQL> select * from t1;
1024000 rows selected.
Elapsed: 00:00:07.43
Execution Plan
----------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   512K|    41M|  1785   (1)| 00:00:22 |
|   1 |  TABLE ACCESS FULL| T1   |   512K|    41M|  1785   (1)| 00:00:22 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      26518  consistent gets
      12933  physical reads
          0  redo size
   88150935  bytes sent via SQL*Net to client
       7921  bytes received via SQL*Net from client
        684  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
    1024000  rows processed

SQL> set arraysize 5000
SQL> select * from t1;
1024000 rows selected.
Elapsed: 00:00:07.65
Execution Plan
----------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   512K|    41M|  1785   (1)| 00:00:22 |
|   1 |  TABLE ACCESS FULL| T1   |   512K|    41M|  1785   (1)| 00:00:22 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      26043  consistent gets
      12933  physical reads
          0  redo size
   88088795  bytes sent via SQL*Net to client
       2663  bytes received via SQL*Net from client
        206  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
    1024000  rows processed
SQL>







如下证明执行计划中的Cost (%CPU)等于v$sql_plan.COST
SQL> set autotrace traceonly
SQL> select IMEID1,IMEID2,IMEID3,IMEID4,PALLET,SPARE4 from recordlist where MAINID<100000000;
419680 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 477921739
----------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                      |    10 |  1600 |     5   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| RECORDLIST           |    10 |  1600 |     5   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IX_RECORDLIST_MAINID |  5778 |       |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("MAINID"<100000000)
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      69335  consistent gets
      13843  physical reads
          0  redo size
   20229455  bytes sent via SQL*Net to client
     308282  bytes received via SQL*Net from client
      27980  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     419680  rows processed
SQL>


SQL> select sql_id,OPERATION,OPTIONS,OBJECT_NAME,OPTIMIZER,ID,COST,CARDINALITY,BYTES,CPU_COST,IO_COST from v$sql_plan where sql_id='cndu66r2wpa63' and CHILD_NUMBER=0;
SQL_ID          OPERATION        OPTIONS         OBJECT_NAME          OPTIMIZER   ID COST CARDINALITY   BYTESCPU_COST IO_COST
--------------- ---------------- --------------- -------------------- ---------- --- ---- ----------- ------- -------- -------
cndu66r2wpa63   SELECT STATEMENT                                      FIRST_ROWS   0    5
cndu66r2wpa63   TABLE ACCESS     BY INDEX ROWID  RECORDLIST                        1    5          10    1600    42307       5
cndu66r2wpa63   INDEX            RANGE SCAN      IX_RECORDLIST_MAINID              2    4        5778            30486       4

 






如下,sql执行多次不改变arraysize的情况下,每次的consistent gets不会变
SQL> set timing on
SQL> set linesize 200
SQL> set autotrace traceonly
SQL> select IMEID1,IMEID2,IMEID3,IMEID4,PALLET,SPARE4 from recordlist where MAINID<100000000;
419680 rows selected.
Elapsed: 00:00:02.30
Execution Plan
----------------------------------------------------------
Plan hash value: 477921739
----------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                      |    10 |  1600 |     5   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| RECORDLIST           |    10 |  1600 |     5   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IX_RECORDLIST_MAINID |  5778 |       |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("MAINID"<100000000)
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      69335  consistent gets
          0  physical reads
          0  redo size
   20229455  bytes sent via SQL*Net to client
     308282  bytes received via SQL*Net from client
      27980  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     419680  rows processed

SQL>
SQL>
SQL>
SQL> select IMEID1,IMEID2,IMEID3,IMEID4,PALLET,SPARE4 from recordlist where MAINID<100000000;
419680 rows selected.
Elapsed: 00:00:02.23
Execution Plan
----------------------------------------------------------
Plan hash value: 477921739
----------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                      |    10 |  1600 |     5   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| RECORDLIST           |    10 |  1600 |     5   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IX_RECORDLIST_MAINID |  5778 |       |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("MAINID"<100000000)
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      69335  consistent gets
          0  physical reads
          0  redo size
   20229455  bytes sent via SQL*Net to client
     308282  bytes received via SQL*Net from client
      27980  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
     419680  rows processed

SQL>

你可能感兴趣的:(ORACLE数据库性能优化)