Detailed Oracle execution plan (estimated + real)

1 Overview

What is Oracle's execution plan?

The execution plan is a description of the execution process or access path of a query statement in Oracle. To
put it simply, it is: How does Oracle execute the sql statement.

  • For example, it's like going to a place and planning in advance how to take a car. First take the bus wherever you go before taking the subway, the same is true for Oracle's execution plan, which is to execute SQL step by step.
  • Oracle's execution plan is very complicated. Generally, the execution plan we see is a less expensive execution path selected by Oracle through internal algorithm calculations, just as we have to choose a shortcut wherever we go.

1.1 Mind Map

Insert picture description here

1.2 Concept

2 Implementation plan

2.1 Estimated

The easiest way: View through F5 of pl/sql developer:
Insert picture description here

2.2 Real

Prerequisite description:

1.该用户(如:scott)要有访问动态视图的权限(最常用的用户:system)
grant select any dictionary to scott;

Specific steps:

1. 获取执行计划的统计信息(两种方式) '下列两条命令必须在同一个窗口执行哦'
alter session set statistics_level = all; -- 推荐
select * from dual;
-- 设置前,可以用下列两种方式查询当前的 参数信息
-- select * from v$parameter t where t.name = 'statistics_level';
-- show parameter statistics_level
或(每个要获取执行计划 sql 都添加下列 hint,比较麻烦):
select /*+ gather_plan_statistics */ * from dual;

2. 找出执行语句的 'sql_id'
select t.*
  from v$sql t
 where t.sql_text like '%select * from%'
 order by t.last_active_time desc;

3. 根据 'sql_id' 查出 '真实执行计划(最近一条)'
select * from table(dbms_xplan.display_cursor('cyfzxc61h3g3r',null,'allstats last')); 

3 example

Get the real execution plan

1. The executed sql statement:

select /*+ gather_plan_statistics*/
 t.*
  from scott.emp t
 where t.empno >= 7782;

2. Find out the'sql_id' of the executed statement:

select t.*
  from v$sql t
 where t.sql_text like '%where t.empno >= 7782%'
 order by t.last_active_time desc;

3. Find out the'real execution plan (the latest one)' according to'sql_id':

select * from table(dbms_xplan.display_cursor('92dkjj0sw6wjr',null,'allstats last'));

Final Results:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 
Connected as system@orcl

SQL> select * from table(dbms_xplan.display_cursor('92dkjj0sw6wjr',null,'allstats last'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID  92dkjj0sw6wjr, child number 0
-------------------------------------
select /*+ gather_plan_statistics*/  t.*   from scott.emp t  where
t.empno >= 7782
Plan hash value: 169057108
------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |      1 |        |      8 |00:00:00.01 |       2 |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |      1 |      5 |      8 |00:00:00.01 |       2 |
|*  2 |   INDEX RANGE SCAN          | PK_EMP |      1 |      5 |      8 |00:00:00.01 |       1 |
------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("T"."EMPNO">=7782)
20 rows selected

SQL> 

Parameter Description:

Starts  	该 sql 执行的次数
E-Rows  	预计返回的行数
A-Rows  	实际返回的行数。可以和 E-Rows 比对,确定哪一步出现了问题
A-Time 	 	每一步实际执行的时间
Buffers 	每一步实际执行的逻辑读或一致性读
Reads		每一步实际执行的物理读
OMem		最优执行模式所需的内存评估值
1Mem		one-pass模式所需的内存评估值
Used_Mem	则为当前操作实际执行时消耗的内存
			括号里面为(发生磁盘交换的次数,1次即为One-Pass,大于1次则为Multi_Pass,如果没有使用磁盘,则显示0)

4 Alternative commands

SQL> set col 100  -- 设置显示 100 列(若命令窗口显示不全时使用)
SQL>
SQL> clear -- 清屏

Guess you like

Origin blog.csdn.net/qq_34745941/article/details/106068346