图片版排版好看一些:https://blog.csdn.net/chen__peng/article/details/104529919
Oracle用来执行目标SQL语句的步骤的组合被称为执行计划。在Oracle数据库中,通常使用如下这些方法查看执行计划:
1、explain plan for命令
2、SQLPLUS中的AUTOTRACE开关
3、使用DBMS_XPLAN包
4、AWR报告
5、10046事件
6、10053事件
7、其他等
一、使用explain plan for命令查看执行计划:
explain plan for SQL语句; select * from table(dbms_xplan.display); |
explain plan for 方式获取sql的执行计划查看的是最近的Plan table中的信息:当我们使用explain plan命令后,Oracle就将解析SQL所产生的执行计划写入PLAN_TABLE中,随后执行"select * from table(dbms_xplan.display)"是从PLAN_TABLE中将这些具体的执行步骤以格式化的方式显示出来。PLAN_TABLE为一个全局临时表,各个Session之间互不干扰。
此方法查看的是预估的执行计划并非真实的执行计划。
SQL> create table test_plan as select * from dba_objects;
SQL> explain plan for select * from test_plan;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 289916773
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 91094 | 17M| 336 (1)| 00:00:05 |
| 1 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 17M| 336 (1)| 00:00:05 |
-------------------------------------------------------------------------------
Note
-----
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
- dynamic sampling used for this statement (level=2)
12 rows selected.
查看高级的执行计划:
SQL> select * from table(dbms_xplan.display(NULL,NULL,'advanced'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 289916773
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 91094 | 17M| 336 (1)| 00:00:05 |
| 1 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 17M| 336 (1)| 00:00:05 |
-------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - SEL$1 / TEST_PLAN@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
FULL(@"SEL$1" "TEST_PLAN"@"SEL$1")
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
DB_VERSION('11.2.0.4')
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "TEST_PLAN"."OWNER"[VARCHAR2,30],
"TEST_PLAN"."OBJECT_NAME"[VARCHAR2,128],
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------
"TEST_PLAN"."SUBOBJECT_NAME"[VARCHAR2,30],
"TEST_PLAN"."OBJECT_ID"[NUMBER,22],
"TEST_PLAN"."DATA_OBJECT_ID"[NUMBER,22],
"TEST_PLAN"."OBJECT_TYPE"[VARCHAR2,19], "TEST_PLAN"."CREATED"[DATE,7],
"TEST_PLAN"."LAST_DDL_TIME"[DATE,7],
"TEST_PLAN"."TIMESTAMP"[VARCHAR2,19], "TEST_PLAN"."STATUS"[VARCHAR2,7],
"TEST_PLAN"."TEMPORARY"[VARCHAR2,1],
"TEST_PLAN"."GENERATED"[VARCHAR2,1],
"TEST_PLAN"."SECONDARY"[VARCHAR2,1],
"TEST_PLAN"."NAMESPACE"[NUMBER,22],
"TEST_PLAN"."EDITION_NAME"[VARCHAR2,30]
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
48 rows selected.
二、使用AUTOTRACE查看执行计划:
在SQLPLUS中将AUTOTRACE开关打开也可以得到执行计划,还能得到目标SQL在执行时的资源消耗,如:物理读、逻辑读、产生的redo数量、及排序的数量等。
此方法查看的是预估的执行计划并非真实的执行计划,运行结果和SQL运行所消耗的资源情况是真实的。
命令 |
简写 |
|
set autotrace on |
set autot on |
运行SQL并显示运行结果、执行计划和该SQL运行所消耗的资源情况。 |
set autotrace traceonly |
set autot trace |
运行SQL并显示执行计划和该SQL运行所消耗的资源情况。 |
set autotrace traceonly explain |
set autot trace exp |
查询语句不执行,DML语句执行,只显示执行计划。 |
set autotrace traceonly statistics |
set autot trace stat |
只显示该SQL运行所消耗的资源情况。 |
set autotrace off |
set autot off |
关闭AUTOTRACE |
SQL> set autot on
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
OBJECT_ID OBJECT_NAME
---------- --------------------------------------------------------------------------------------------------------------------------------
20 ICOL$
Execution Plan
----------------------------------------------------------
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 79 | 2 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
127 physical reads
0 redo size
606 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot trace
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
Execution Plan
----------------------------------------------------------
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 79 | 2 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
127 physical reads
0 redo size
606 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot trace exp
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
Execution Plan
----------------------------------------------------------
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 79 | 2 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Note
-----
- dynamic sampling used for this statement (level=2)
SQL> set autot trace stat
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
127 physical reads
0 redo size
606 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot off
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
OBJECT_ID OBJECT_NAME
---------- ----------------------------------------
20 ICOL$
三、使用DBMS_XPLAN包查看执行计划:
select * from table(dbms_xplan.display); select * from table(dbms_xplan.display(NULL,NULL,'advanced')); |
查看explain plan for SQL语句执行后,存放在PLAN_TABLE中的执行计划, 查看的是预估的执行计划并非真实的执行计划 |
select * from table(dbms_xplan.display_cursor(NULL,NULL,'advanced/all')); |
用于在SQLPLUS中查看刚刚执行过的SQL的执行计划,一般多使用advanced参数, 查看的是存放在Shared Pool中的执行计划,因此查看的是真实的执行计划 |
select * from table(dbms_xplan.display_cursor('sql_id/hash_value',child_cursor_number)); select * from table(dbms_xplan.display_cursor('sql_id/hash_value',child_cursor_number,'advanced/all')); |
查看指定SQL_ID/HASH_VALUE的执行计划, 查看的是存放在Shared Pool中的执行计划,因此查看的是真实的执行计划 |
select * from table(dbms_xplan.display_awr('sql_id')); |
查看被采集到AWR中的SQL的执行计划,可以用来查看该SQL所有历史的执行计划。 |
1、SQL> select * from table(dbms_xplan.display);
SQL> select * from table(dbms_xplan.display(NULL,NULL,'advanced'));
2、
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
OBJECT_ID OBJECT_NAME
---------- -------------------------------------------------------------------------------------------------------------------------
20 ICOL$
SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'advanced'));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 4zz2rryg7y29h, child number 0
-------------------------------------
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / TEST_PLAN@SEL$1
Outline Data
-------------
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
DB_VERSION('11.2.0.4')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "TEST_PLAN"@"SEL$1")
END_OUTLINE_DATA
*/
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "OBJECT_NAME"[VARCHAR2,128], "OBJECT_ID"[NUMBER,22]
2 - "OBJECT_NAME"[VARCHAR2,128], "OBJECT_ID"[NUMBER,22]
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
49 rows selected.
3、
SQL> select sql_text,sql_id,hash_value,child_number from v$sql where sql_text like 'select OBJECT_ID,OBJECT_NAME%';
SQL_TEXT SQL_ID HASH_VALUE CHILD_NUMBER
-------------------------------------------------------------------------------- ------------- ---------- ------------
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1 4zz2rryg7y29h 2659125552 0
SQL> select * from table(dbms_xplan.display_cursor('4zz2rryg7y29h',0,'advanced'));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 4zz2rryg7y29h, child number 0
-------------------------------------
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / TEST_PLAN@SEL$1
Outline Data
-------------
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
DB_VERSION('11.2.0.4')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "TEST_PLAN"@"SEL$1")
END_OUTLINE_DATA
*/
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "OBJECT_NAME"[VARCHAR2,128], "OBJECT_ID"[NUMBER,22]
2 - "OBJECT_NAME"[VARCHAR2,128], "OBJECT_ID"[NUMBER,22]
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
49 rows selected.
SQL> select * from table(dbms_xplan.display_cursor('4zz2rryg7y29h',0));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 4zz2rryg7y29h, child number 0
-------------------------------------
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1
Plan hash value: 3247200414
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 91094 | 7027K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Note
-----
- dynamic sampling used for this statement (level=2)
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
23 rows selected.
3、
SQL> exec dbms_workload_repository.create_snapshot();
PL/SQL procedure successfully completed.
SQL> select * from table(dbms_xplan.display_awr('4zz2rryg7y29h'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------
SQL_ID 4zz2rryg7y29h
--------------------
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1
Plan hash value: 3247200414
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | COUNT STOPKEY | | | | | |
| 2 | TABLE ACCESS FULL| TEST_PLAN | 95447 | 7363K| 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
18 rows selected.
查看历史的执行计划:
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20;
OBJECT_ID OBJECT_NAME
---------- ----------------------------------------------------------------------------------------------------------------------------
20 ICOL$
SQL> select sql_text,sql_id,hash_value,child_number from v$sql where sql_text like 'select OBJECT_ID,OBJECT_NAME%';
SQL_TEXT SQL_ID HASH_VALUE CHILD_NUMBER
------------------------------------------------------------------------------------------ ------------- ---------- ------------
select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20 1pdhryb697urw 3432246012 0
SQL> select * from table(dbms_xplan.display_cursor('1pdhryb697urw',0));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
SQL_ID 1pdhryb697urw, child number 0
-------------------------------------
select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20
Plan hash value: 289916773
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 312 (100)| |
|* 1 | TABLE ACCESS FULL| TEST_PLAN | 18 | 1422 | 312 (1)| 00:00:04 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
Note
-----
- dynamic sampling used for this statement (level=2)
22 rows selected.
SQL> create index idx_plan_id on test_plan(object_id);
Index created.
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20;
OBJECT_ID OBJECT_NAME
---------- ----------------------------------------------------------------------------------------------------------------------------
20 ICOL$
SQL> select * from table(dbms_xplan.display_cursor('1pdhryb697urw',0));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
SQL_ID 1pdhryb697urw, child number 0
-------------------------------------
select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20
Plan hash value: 44098703
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST_PLAN | 1 | 79 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_PLAN_ID | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=20)
Note
-----
- dynamic sampling used for this statement (level=2)
23 rows selected.
SQL> exec dbms_workload_repository.create_snapshot();
SQL> alter system flush shared_pool;
System altered.
SQL>select * from table(dbms_xplan.display_awr('1pdhryb697urw'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
SQL_ID 1pdhryb697urw
--------------------
select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20
Plan hash value: 44098703
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST_PLAN | 1 | 79 | 2 (0)| 00:00:01 |
| 2 | INDEX RANGE SCAN | IDX_PLAN_ID | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
SQL_ID 1pdhryb697urw
--------------------
select OBJECT_ID,OBJECT_NAME from test_plan where object_id=20
Plan hash value: 289916773
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 312 (100)| |
| 1 | TABLE ACCESS FULL| TEST_PLAN | 18 | 1422 | 312 (1)| 00:00:04 |
-------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
35 rows selected.
四、使用AWR查看执行计划:
与DBMS_XPLAN.DISPLAY_AWR一样,查看的被采集到AWR中的SQL的执行计划,可以用来查看该SQL所有历史的执行计划。
查看的是真实的执行计划,但在数据转储时并未保留谓词条件的信息
SQL> exec dbms_workload_repository.create_snapshot();
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
OBJECT_ID OBJECT_NAME
---------- ----------------------------------------------------------------------------------------------------------------------------
20 ICOL$
SQL> exec dbms_workload_repository.create_snapshot();
SQL> @?/rdbms/admin/awrsqrpt.sql
Current Instance
~~~~~~~~~~~~~~~~
DB Id DB Name Inst Num Instance
----------- ------------ -------- ------------
1406181968 ORCL 1 orcl
Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~~
Would you like an HTML report, or a plain text report?
Enter 'html' for an HTML report, or 'text' for plain text
Defaults to 'html'
Enter value for report_type:
Type Specified: html
Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DB Id Inst Num DB Name Instance Host
------------ -------- ------------ ------------ ------------
* 1406181968 1 ORCL orcl oracle
Using 1406181968 for database Id
Using 1 for instance number
Specify the number of days of snapshots to choose from
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing
specifying a number lists all completed snapshots.
Enter value for num_days: 1
Listing the last day's Completed Snapshots
Snap
Instance DB Name Snap Id Snap Started Level
------------ ------------ --------- ------------------ -----
orcl ORCL 125 27 Feb 2020 07:39 1
126 27 Feb 2020 07:41 1
127 27 Feb 2020 07:44 1
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 126
Begin Snapshot Id specified: 126
Enter value for end_snap: 127
End Snapshot Id specified: 127
Specify the SQL Id
~~~~~~~~~~~~~~~~~~
Enter value for sql_id: 4zz2rryg7y29h
SQL ID specified: 4zz2rryg7y29h
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrsqlrpt_1_126_127.html. To use this name,
press
Enter value for report_name:
Using the report name awrsqlrpt_1_126_127.html
五、使用10046与10053查看执行计划:
使用10046事件查看执行计划,能够得到SQL执行计划中每一个执行步骤所消耗的逻辑读,物理读,和花费的时间。
10046所查看到的执行计划是真实的执行计划。
1. 激活当前 session 10046 事件
|
SQL>alter session set events '10046 trace name context forever,level 12'; ------------------------------------------------------------------------------------------ SQL> oradebug setmypid SQL> oradebug event 1046 trace name context forever,level 12; SQL> oradebug tracefile_name |
2. 在当前 session 中执行目标 sql |
|
3. 关闭当前 session 10046 事件 |
SQL> alter session set events '10046 trace name context off'; ------------------------------------------------------------------------------------------ SQL> oradebug event 10046 trace name context off; |
SQL> alter session set events '10046 trace name context forever,level 12';
Session altered.
SQL> select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1;
OBJECT_ID OBJECT_NAME
---------- ----------------------------------------------------------------------------------------------------------------------------
20 ICOL$
SQL> alter session set events '10046 trace name context off';
Session altered.
SQL> select tracefile from v$process where addr in (select paddr from v$session where sid in (select sid from v$mystat));
TRACEFILE
------------------------------------------------------------------------------------------------------------------------
/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_34331.trc
[oracle@oracle ~]$ more /oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_34331.trc
Trace file /oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_34331.trc
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORACLE_HOME = /oracle/product/10.2.0/db_1
System name: Linux
Node name: oracle
Release: 2.6.32-279.el6.x86_64
Version: #1 SMP Wed Jun 13 18:24:36 EDT 2012
Machine: x86_64
VM name: VMWare Version: 6
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 27
Unix process pid: 34331, image: oracle@oracle (TNS V1-V3)
*** 2020-02-27 08:13:44.297
*** SESSION ID:(49.1371) 2020-02-27 08:13:44.297
*** CLIENT ID:() 2020-02-27 08:13:44.297
*** SERVICE NAME:(SYS$USERS) 2020-02-27 08:13:44.297
*** MODULE NAME:(sqlplus@oracle (TNS V1-V3)) 2020-02-27 08:13:44.297
*** ACTION NAME:() 2020-02-27 08:13:44.297
WAIT #140190017325304: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1582762424297468
*** 2020-02-27 08:14:00.608
WAIT #140190017325304: nam='SQL*Net message from client' ela= 16310989 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1582762440608610
CLOSE #140190017325304:c=0,e=7,dep=0,type=1,tim=1582762440608677
=====================
PARSING IN CURSOR #140190017319528 len=58 dep=0 uid=0 oct=3 lid=0 tim=1582762440608749 hv=2659125552 ad='75c61560' sqlid='4zz2rryg7y29h'
select OBJECT_ID,OBJECT_NAME from test_plan where rownum=1
END OF STMT
PARSE #140190017319528:c=0,e=52,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=3247200414,tim=1582762440608746
EXEC #140190017319528:c=0,e=27,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=3247200414,tim=1582762440608801
WAIT #140190017319528: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1582762440608817
WAIT #140190017319528: nam='Disk file operations I/O' ela= 39 FileOperation=2 fileno=1 filetype=2 obj#=80051 tim=1582762440609139
WAIT #140190017319528: nam='direct path read' ela= 73 file number=1 first dba=79193 block cnt=7 obj#=80051 tim=1582762440626876
WAIT #140190017319528: nam='direct path read' ela= 131 file number=1 first dba=79200 block cnt=32 obj#=80051 tim=1582762440627055
FETCH #140190017319528:c=999,e=18257,p=39,cr=4,cu=0,mis=0,r=1,dep=0,og=1,plh=3247200414,tim=1582762440627085
STAT #140190017319528 id=1 cnt=1 pid=0 pos=1 obj=0 op='COUNT STOPKEY (cr=4 pr=39 pw=0 time=18254 us)'
STAT #140190017319528 id=2 cnt=1 pid=1 pos=1 obj=80051 op='TABLE ACCESS FULL TEST_PLAN (cr=4 pr=39 pw=0 time=18251 us cost=2 size=7540313 card=95447)'-----<微秒 1s=1,000,000us>
WAIT #140190017319528: nam='SQL*Net message from client' ela= 122 driver id=1650815232 #bytes=1 p3=0 obj#=80051 tim=1582762440627288
FETCH #140190017319528:c=0,e=11,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,plh=3247200414,tim=1582762440627346
WAIT #140190017319528: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=80051 tim=1582762440627367
*** 2020-02-27 08:14:06.064
WAIT #140190017319528: nam='SQL*Net message from client' ela= 5437295 driver id=1650815232 #bytes=1 p3=0 obj#=80051 tim=1582762446064667
CLOSE #140190017319528:c=0,e=10,dep=0,type=1,tim=1582762446064738
=====================
PARSING IN CURSOR #140190017518872 len=55 dep=0 uid=0 oct=42 lid=0 tim=1582762446064795 hv=2217940283 ad='0' sqlid='06nvwn223659v'
alter session set events '10046 trace name context off'
END OF STMT
PARSE #140190017518872:c=0,e=37,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,plh=0,tim=1582762446064794
EXEC #140190017518872:c=0,e=147,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,plh=0,tim=1582762446064963
使用tkprof可以格式化trace文件
[oracle@oracle trace]$ tkprof orcl_ora_34331.trc orcl_ora_34331.txt
TKPROF: Release 11.2.0.4.0 - Development on Thu Feb 27 08:16:52 2020
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
[oracle@oracle trace]$ cat orcl_ora_34331.txt
TKPROF: Release 11.2.0.4.0 - Development on Thu Feb 27 08:16:52 2020
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Trace file: orcl_ora_34331.trc
Sort options: default
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
SQL ID: 4zz2rryg7y29h Plan Hash: 3247200414
select OBJECT_ID,OBJECT_NAME
from
test_plan where rownum=1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.01 39 4 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.01 39 4 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
---------- ---------- ---------- ---------------------------------------------------
1 1 1 COUNT STOPKEY (cr=4 pr=39 pw=0 time=18254 us)
1 1 1 TABLE ACCESS FULL TEST_PLAN (cr=4 pr=39 pw=0 time=18251 us cost=2 size=7540313 card=95447)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
Disk file operations I/O 1 0.00 0.00
direct path read 2 0.00 0.00
SQL*Net message from client 2 5.43 5.43
********************************************************************************
SQL ID: 06nvwn223659v Plan Hash: 0
alter session set events '10046 trace name context off'
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Parsing user id: SYS
********************************************************************************
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 2 0.00 0.00 0 0 0 0
Execute 2 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.01 39 4 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 6 0.00 0.01 39 4 0 1
Misses in library cache during parse: 0
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 3 0.00 0.00
SQL*Net message from client 3 16.31 21.74
Disk file operations I/O 1 0.00 0.00
direct path read 2 0.00 0.00
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 0 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 0 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
2 user SQL statements in session.
0 internal SQL statements in session.
2 SQL statements in session.
********************************************************************************
Trace file: orcl_ora_34331.trc
Trace file compatibility: 11.1.0.7
Sort options: default
1 session in tracefile.
2 user SQL statements in trace file.
0 internal SQL statements in trace file.
2 SQL statements in trace file.
2 unique SQL statements in trace file.
54 lines in trace file.
5 elapsed seconds in trace file.
10053事件与10046事件的使用方法类似,使用10053查看执行计划,能够得到Oracle是如何做出执行计划选择的。查看的是真实的执行计划。
SQL> ALTER SESSION SET EVENTS='10053 trace name context forever, level 1';
Session altered.
SQL> ALTER SESSION SET EVENTS='10053 trace name context forever, level 2';
Session altered.
关闭:
SQL> ALTER SESSION SET EVENTS '10053 trace name context off';
Session altered.