通过SQLPLUS中的autotrace命令查看执行计划

通过SQL*PLUS中的autotrace命令查看

            1.登录拥有dba权限的用户,分别执行

               脚本 ${ORACLE_HOME}/RDBMS/ADMIN/utlxplan.sql和

               脚本 ${ORACLE_HOME}/sqlplus/admin/plustrce.sql

               然后通过SQL*PLUS就可以查看执行计划了

            2.查看执行计划有下面四种选项

                1> set autotrace on    --(得到执行计划,并输出结果)

                2> set autotrace traceonly    --(得到执行计划,但不输出结果)

                3> set autotrace traceonly explain    --(得到执行计划,不输出统计信息和结果,仅展现执行计划部分)

                4> set autotrace traceonly statistics    --(不输出执行计划和结果,仅展现统计信息)

            3.优缺点

                优点:

                    1> 可以输出运行时的相关统计信息(产生多少逻辑读,多少递归调用,多少次物理读的情况)

                    2> 虽然必须要等语句执行完毕后才可以执行计划,但是可以有traceonly开关来控制返回结果不打屏输出

                缺点:

                    1> 必须等语句执行完毕后,才可以出结果

                    2> 无法看到表被访问了多少次

方法三、explain plan for获取

            1.执行步骤如下:

                SQL> set linesize 200;

                SQL> set pagesize 2000;

                SQL> explain plan for select * from emp;

                Explained

                SQL> select * from table(dbms_xplan.display());

                PLAN_TABLE_OUTPUT

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

                Plan hash value: 3956160932

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

                | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

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

                |   0 | SELECT STATEMENT  |      |    14 |   532 |     3   (0)| 00:00:01 |

                |   1 |  TABLE ACCESS FULL| EMP  |    14 |   532 |     3   (0)| 00:00:01 |

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

                8 rows selected

            2.优缺点

                优点:

                    1> 无需真正执行,快捷方便

                缺点:

                    1> 没有输出相关的统计信息(产生多少次逻辑读,多少次物理读,多少次递归调用)

                    2> 无法判断处理了多少行

                    3> 无法判断表被访问了多少次

方法四、statistics_level = all 或者 /*+ gather_plan_statistics */

            1.执行步骤

                1> 通过statistics_level = all

                     SQL> set linesize 500;

                     SQL> set pagesize 1000;

                     SQL> alter session set statistics_level = all;

                     会话已更改。

                     SQL> select count(*) from emp;

                       COUNT(*)

                     ----------

                             14

                     SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

                     PLAN_TABLE_OUTPUT

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

                     SQL_ID  g59vz2u4cu404, child number 0

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

                     select count(*) from emp

                     Plan hash value: 2937609675

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

                     | Id  | Operation        | Name   | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

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

                     |   0 | SELECT STATEMENT |        |      1 |        |      1 |00:00:00.01 |       1 |

                     |   1 |  SORT AGGREGATE  |        |      1 |      1 |      1 |00:00:00.01 |       1 |

                     |   2 |   INDEX FULL SCAN| PK_EMP |      1 |     14 |     14 |00:00:00.01 |       1 |

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

                     已选择14行。

                2> 通过 /*+ gather_plan_statistics*/

                     SQL> set linesize 200;

                     SQL> set pagesize 500;

                     SQL> select /*+gather_plan_statistics*/ count(*) from emp;

                       COUNT(*)

                     ----------

                             14

                     SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

                     PLAN_TABLE_OUTPUT

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

                     SQL_ID  537ffv2mq5375, child number 0

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

                     select /*+gather_plan_statistics*/ count(*) from emp

                     Plan hash value: 2937609675

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

                     | Id  | Operation        | Name   | Starts | E-Rows | A-Rows |   A-Time   | Buffers |

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

                     |   0 | SELECT STATEMENT |        |      1 |        |      1 |00:00:00.01 |       1 |

                     |   1 |  SORT AGGREGATE  |        |      1 |      1 |      1 |00:00:00.01 |       1 |

                     |   2 |   INDEX FULL SCAN| PK_EMP |      1 |     14 |     14 |00:00:00.01 |       1 |

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

                     已选择14行。

            2.关键字解读

                1> Starts:该SQL执行的次数

                2> E-Rows:执行计划预计的行数。

                3> A-Rows:实际返回的行数。A-Rows跟E-Rows做比较,就可以确定哪一步执行计划出了问题。

                4> A-Time:每一步实际执执行的时间(HH : MM :SS.FF),根据这一行可以知道该SQL耗时在了哪个地方

                5> Buffers:每一步执行的逻辑读或一致性读

            3.优缺点

                优点

                    1> 可以清晰的从Starts得出表被访问多少

                    2> 可以清晰的从E-Rows和A-Rows中得到预测的行数和真实的行数,从而可以准确判断Oracle评估是否正确

                    3> 虽然没有专门的输出运行时的相关统计信息,但是执行计划中的buffers就是真实的逻辑读的多少

                缺点

                    1> 必须等语句真正执行完毕后,才可以得出结果

                    2> 无法控制记录输屏打出,不像autotace由traceonly可以不将结果打屏输出

                    3> 看不出递归调用次数,看不出物理读的多少(不过逻辑读才是重点)

方法五、通过dbms_xplan.display_cursor(&sql_id)输入sql_id参数获取

            1.执行步骤

                 SQL> select sql_id from sys.v_$sql t where t.sql_text like '%select count(*) from emp';

                 SQL_ID

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

                 g59vz2u4cu404

                 SQL> select * from table(dbms_xplan.display_cursor('g59vz2u4cu404'));

                 PLAN_TABLE_OUTPUT   

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

                 SQL_ID  g59vz2u4cu404, child number 0

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

                 select count(*) from emp

                

                 Plan hash value: 2937609675

                

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

                 | Id  | Operation        | Name   | Rows  | Cost (%CPU)| Time     |

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

                 |   0 | SELECT STATEMENT |        |       |     1 (100)|          |

                 |   1 |  SORT AGGREGATE  |        |     1 |            |          |

                 |   2 |   INDEX FULL SCAN| PK_EMP |    14 |     1   (0)| 00:00:01 |

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

                 已选择14行。

            2.优缺点

                优点:

                    1> 知道sql_id立即可得到执行计划,和explain plan for一样无需执行

                    2> 可以得到真实的执行计划

                缺点:

                    1> 没有输出运行的相关统计信息(产生的物理读,逻辑读,递归调用次数)

                    2> 无法判断处理了多少行

                    3> 无法判断表被访问了多少次

方法六、10046 trace跟踪

            1.执行步骤 

                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  (格式化命令)

                

                 SQL> set autot off

                 SQL> alter session set statistics_level=typical;

                 Session altered.

                 SQL> alter session set events '10046 trace name context forever,level 12';

                 Session altered.

                 SQL> select count(*) from test;

                   COUNT(*)  

                 ----------

                          7

                 SQL> alter session set events '10046 trace name context off';

                 Session altered.

                 SQL> select d.value

                

                   2  || '/'

                

                   3  || LOWER (RTRIM(i.INSTANCE, CHR(0)))

                

                   4  || '_ora_'

                

                   5  || p.spid

                

                   6  || '.trc' trace_file_name

                

                   7  from (select p.spid

                

                   8        from v$mystat m,v$session s, v$process p

                

                   9        where  m.statistic#=1 and s.sid=m.sid and p.addr=s.paddr) p,

                

                  10        (select t.INSTANCE

                

                  11         FROM v$thread t,v$parameter v

                

                  12         WHERE v.name='thread'

                

                  13         AND(v.VALUE=0 OR t.thread#=to_number(v.value))) i,

                

                  14         (select value

                

                  15         from v$parameter

                

                  16         where name='user_dump_dest') d;

 

                 TRACE_FILE_NAME

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

                

                 /u01/app/oracle/diag/rdbms/ora12c/ora12c/trace/ora12c_ora_12195.trc

                 SQL> host

                 [oracle@ora12c ~]$ tkprof /u01/app/oracle/diag/rdbms/ora12c/ora12c/trace/ora12c_ora_12195.trc /home/oracle/10046.txt sys=no sort=prsela,exeela,fchela

 

                 TKPROF: Release 12.1.0.1.0 - Development on Fri Jan 20 08:22:25 2017

 

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

 

                 SQL ID: 7b2twsn8vgfsc Plan Hash: 784602781

 

                 select count(*)     from  test

                 call     count       cpu    elapsed       disk      query    current        rows

                

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

                

                 Parse        1      0.00       0.00          3          3          2           0

                

                 Execute      1      0.00       0.00          0          0          0           0

                

                 Fetch        2      0.00       0.00          4         66          0           1

                

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

                

                 total        4      0.00       0.00          7         69          2           1

                

                 Misses in library cache during parse: 1

                

                 Optimizer mode: ALL_ROWS

                

                 Parsing user id: 103  

                

                 Number of plan statistics captured: 1

                

                 Rows (1st) Rows (avg) Rows (max)  Row Source Operation

                

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

                

                          1          1          1  SORT AGGREGATE (cr=66 pr=4 pw=0 time=298 us)

                

                          7          7          7   PARTITION RANGE ALL PARTITION: 1 3 (cr=66 pr=4 pw=0 time=397 us cost=39 size=0 card=11)

                

                          7          7          7    TABLE ACCESS FULL TEST PARTITION: 1 3 (cr=66 pr=4 pw=0 time=290 us cost=39 size=0 card=11)

                

                 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

                

                   SQL*Net message to client                       2        0.00          0.00

                

                   db file scattered read                          1        0.00          0.00

                

                   SQL*Net message from client                     2        7.03          7.03

            2.优缺点

                优点:

                    1> 可以看出SQL语句对应的等待事件

                    2> 如果SQL语句中有函数,SQL中有SQL,将会都被列出,无处遁形

                    3> 可以方便的看出处理的行数,逻辑物理读

                    4> 可以跟踪整个程序包

                缺点:    

                    1> 步骤繁琐,比较麻烦

                    2> 无法判断表被访问了多少次

                    3> 执行计划中的谓词部分不能清晰的展现出来

方法七、awrsqlrpt.sql

            1.执行步骤  

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

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

                Step3:输入sql_id

适用场合分析

    1.如果某SQL执行非常长时间才会出结果,甚至慢到返回不了结果,这时候看执行计划就只能用方法explain plan for;

    2.跟踪某条SQL最简单的方法是方法explain plan for ,其次就是方法autotrace;

    3.如果想观察到某条SQL有多条执行计划的情况,只能用方法dbms_xplan.display_cursor(sql_id)和方法awrsqlrpt.sql;

    4.如果SQL中含有多函数,函数中套有SQL等多层递归调用,想准确分析,只能使用方法10046 trace;

    5.要想确保看到真实的执行计划,不能用方法plsql developer和方法explain plan for;

    6.要想获取表被访问的次数,只能使用方法statistics_level(/*+ gather_plan_statistics */);

 

 

 

通过SQLPLUS中的autotrace命令查看执行计划通过SQLPLUS中的autotrace命令查看执行计划 星星点点的绿光 发布了20 篇原创文章 · 获赞 3 · 访问量 2219 私信 关注
上一篇:Linux下启动oracle


下一篇:sqlplus登录时密码有特殊符号解决方法