Oracle Implementation Plan 2 (Explain Plan)

First, the method of obtaining execution plan

(1) explain plan for

step:

  • 1: explain plan for your SQL;
  • 2:select * from table (dbms_xplan. display()) ;
  • Pros: Really do not need to perform, fast and convenient
  • Disadvantages: No statistics (read logic, recursive calls, physical reads) output runtime, because there is no real implementation, so you can not see the implementation of a number of rows, the table has been visited many times and so on

(2) set autotrace on

sqlplus login:

用户名/密码@主机名称:1521/数据库名

step:

  • 1:set sutoatrace on
  • 2: In the implementation of your sql;
  • Pros: If statistics runtime (read logic, recursive calls, physical reads)
  • Disadvantages: can not see how many times the table is accessed, also need to wait to see the completion of the implementation of sql

(3) statistics_level=all

step:

  • 1:alter session set statistics_level=all;
  • 2: Execute your SQL here;
  • 3:select * from table(dbms_xplan.display_cursor(null , null,'allstats last'));

If the syntax used Hint: / * + gather_plan_statistics * /, a step can be omitted, directly to steps 2 and 3, acquires the execution plan

Keyword Interpretation:

  • Starts: number of times the execution of SQL
  • E-Rows: Estimated number of rows for the implementation plan
  • A-Rows: The actual number of rows returned, E-Rows and A-Rows for comparison, you can see specific problems with that step implementation plan
  • A-Time: every step of the actual execution time, you can see time-consuming SQL
  • Buffers: logical reads every step of the actual implementation or read consistency
  • Reads: Physical reads
  • OMem: the current operation to complete all work area of ​​memory operation using private memory size of the total work area (PGA) of
  • lMem: When the working set size characters can not meet the needs of the size of the operation, part of the data needs to be written to a temporary disk space (if need be written only once to complete the operation, it said once through, One-Pass; otherwise, many times by, Multi-pass). Change data for the last statement execution, a single disk write the required memory size, this is by the optimizer statistics and performance data from a previous estimate derived from the implementation of
  • Used-Mem: statement last execution in the current work area of memory used by the size of the operation, which is in brackets (the number of disk swapping occurs, 1 is the One-Pass, more than one time was Mullti-Pass, if no disk as OPTI1MAL) displays
    memory OMem, lMem memory is necessary to execute the evaluation value, the evaluation value for the optimal execution Omem desired mode memory, Used-Mem is consumed

advantage:

  • STARTS can be drawn from the table is accessed many times;
  • Real number of rows and number of rows can be clearly derived from the predicted E-ROWS, respectively, and A-ROWS
    disadvantages:
  • Must wait until after the statement is actually executed is completed, the results can only be obtained
  • Unable to control screen output hit record, you do not want to have traceonly command aututrace
  • No specific statistics on output, not to see the number of recursive calls, do not see the physical read specific values, but there is logic to read, read logic is the key

(4) dbms_xplan.display_cursor get

Step
acquired from the shared pool

//${SQL_ID}参数可以从共享池拿
select * from table(dbms_xplan.display_cursor(${SQL_ID}));

You can also get a view from inside AWR performance

select * from table(dbms_xplan.display_awr(${SQL_ID}));

Multiple implementation plan can be found in a similar manner

select * from table(dbms_xplan.display_cursor(${SQL_ID},0));

select * from table(dbms_xplan.display_cursor(${SQL_ID},1));

advantage:

  • And really do not need to explain as to perform, like to know sql_id

Disadvantages:

  • We can not determine how many rows processed
  • We can not determine how many times the table is accessed
  • No statistics (read logic, recursive calls, physical reads) output runtime

(5) event 10046 trace track

step:

1:alter session set events '10046 trace name context forever,level 12';//开启跟踪
2:执行你的语句
3:alter session set events '10046 trace name context off';//关闭跟踪
4:找到跟踪产生的文件
5:tkprof trc文件 目标文件 sys=no sort=prsela,exeela,fchela(格式化命令)

advantage:

  • SQL statement can be seen in the corresponding wait event
  • You can list the function call of sql statement
  • It can be seen parsing events and perform event
  • You can track the entire package
  • Processing logic can be seen that the number of rows, the resulting read
    disadvantages:
  • More complicated steps
  • We can not determine how many times the table is accessed
  • Predicate part of the implementation plan can not be clearly displayed

(6) awrsqrpt.sql

step:

1:@?/rdbms/admin/awrsqrpt.sql
具体可以参考我之前的博客:https://smilenicky.blog.csdn.net/article/details/89429989 

Second, the interpretation of the classic method of implementation of the plan

It can be divided into two types: a single type and combined type

Joint type divided into: joint type and joint type associated with the non-associated

[Type] alone

Alone type is better understood, the order is executed in accordance with id = 1, id = 2, id = 3 execution, from far and near
to scott login, and then execute sql, examples from the "harvest, more than SQL optimization," a book

select deptno, count(*)
  from emp
 where job = 'CLERK'
   and sal < 3000
 group by deptno
 
Here Insert Picture Description

It can be given a separate type Legend:


 
Here Insert Picture Description

[Relational] Joint type

(1) associative type association (NL)

As used herein Hint of nl

select /*+ ordered use_nl(dept) index(dept) */ *
  from emp, dept
 where emp.deptno = dept.deptno
   and emp.comm is null
   and dept.dname != 'SALES' 

This map from the "harvest, more than SQL optimization" can be seen as A-Rows practice id 2 returns the number of rows 10, id is Starts 3 to 10, illustrating the driving emp table accessed returned result sets the number of records, is driving table was visited many times , this is the type associated with the salient features

 
Here Insert Picture Description

 
Here Insert Picture Description

 

 

 
Here Insert Picture Description

Association type is not necessarily how many drivers return to the table, the table was driven How many visits, pay attention FILTER mode is also associated with type

 

(2) association associative type (the FILTER)

It has been introduced joint-type association type (nl) of this method, this method is the driving table to return the number of records, driven table was visited many times, but this situation does not apply to the lower FILTER mode

Implementation of SQL, used here Hint / * + no_unnset * /

select * from emp where not exists (select /*+ no_unnset */ 0 from dept 
where dept.dname='SALES' and dept.deptno = emp.deptno) and not exists(select /*+ no_unnset */ 0 from bonus where bonus.ename = emp.ename) 

ps: FIG from the "harvest, more than SQL optimization", which can be seen here where id is 2, A-Rows returns the actual number of rows is 8, and the local id 3, Starts to 3, corresponding to the SQL execution instructions 3 times, that is driven dept table has been visited three times, and this has just been introduced in different ways nl, why different?


 
Here Insert Picture Description

Inquiries about SQL, you can see the actual return 3, the other is repeated many,

select dname, count(*) from emp, dept where emp.deptno = dept.deptno group by dname;

So, it is obvious, filtered duplicate data, that data filtering FILTER mode, the driver returns the result set table execution how many rows does not duplicate data, driven table was visited many times, FILTER mode can be said to improve nl mode

(3) association associative type (the UPDATE)

update emp e1 set sal = (select avg(sal) from emp e2 where e2.deptno = e1.deptno),comm = (select avg(comm) from emp e3)

Associative type association (the UPDATE) and the like FILTER mode, it will not repeat

(4) associative type association (CONNECT BY WITH FILTERING)

select /*+ connect_by_filtering */ level, ename ,prior
ename as manager from emp start with mgr is null connect by prior empno = mgr 

Given associative relational Legend:


 
Here Insert Picture Description

[Non-associated joint type type]

You can execute SQL

select ename from emp union all select dname from dept union all select '%' from dual

For plsql can use the tool to view the execution plan, sqlplus client can use all statistics_level = method of obtaining execution plan, specific steps

  • 1:alter session set statistics_level=all;
  • 2: Execute your SQL here;
  • 3:select * from table(dbms_xplan.display_cursor(null , null,'allstats last'));
 
Here Insert Picture Description

Joint type can be given non-associated type of legend:


 
Here Insert Picture Description


Author: smileNicky
link: https: //www.jianshu.com/p/bfb62aceebd7
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/min-yu/p/11454107.html