The method for generating and displaying execution plans in Oracle. In addition to the executed SQL statements, the execution plan is generated in the memory, and the explain Plan can be used to enable the optimizer to parse the SQL statements,
The method for generating and displaying execution plans in Oracle. In addition to the executed SQL statements, the execution plan is generated in the memory, and the explain plan can be used to enable the optimizer to parse the SQL statements,
Generate execution plan
After a valid statement is executed, at least one execution plan is generated in the memory, which can be queried from the view v $ SQL _plan. Each execution plan has a cursor. The CHILD_NUMBER (that is, the CHILD_NUMBER in v $ SQL _plan) of the First cursor produced by a statement is 0. Different execution plans may be generated for the same SQL statement due to other factors such as the Environment or version, that is to say, an SQL statement may have multiple child_numbers.
In addition to the executed SQL statements, an execution plan is generated in the memory. You can also use the explain plan to enable the optimizer to parse the SQL statements and generate a query plan. After the explain plan command is executed, Oracle inserts the explain execution plan into sys. plan_table $ (before 10 Gb, the table name is plan_table; after 10 Gb, point to sys through the public synonym plan_table. plan_table $. The execution plan generated by the explain plan is not reused during statement execution, but similar to the explain plan In the cache.
Show execution plan
Use the DBMS_XPLAN package to display the Execution Plan (recommended)
DBMS_XPLAN contains five functions for outputting formatted execution plans:
Display: display the explain plan command explain the execution plan
Display_cursor: displays the execution plan in the memory.
Display_awr: Execution Plan in historical awr data
Display_sqlset: Execution Plan of SQL optimization concentrated statements
Display_ SQL _plan_baseline: SQL Execution Plan baseline
Display usage
Syntax:
DBMS_XPLAN.DISPLAY (table_name IN VARCHAR2 DEFAULT 'Plan _ table', statement_id IN VARCHAR2 default null, formatIN VARCHAR2 DEFAULT 'typical', filter_preds IN VARCHAR2 default null );
Parameters:
ParameterDescription
Table_name
Name of the table storing the query plan. The default value is PLAN_TABLE.
Statement_id
The ID of the SQL statement to display the query plan. If it is null, the latest interpreted statement is obtained.
Format
The output format can be selected or customized based on the following predefined formats (not mentioned here ):
BASIC: the BASIC format with the least content.
TYPICAL: TYPICAL format, which is usually adequate. Default format
SERIAL: SERIAL execution format. This format is basically the same as the output content in a typical format. It only outputs relevant parallel content for parallel queries.
ALL: full format, relatively complete output content
Filter_preds
This parameter uses the predicate condition to filter the tables that store the execution plan, for example, 'Plan _ id = 10 or cost> 100 (show the operation with an estimated cost greater than 100). This value is empty by default.
Example:
Execute the SQL statement using explain plan:
EXPLAIN PLAN
SELECT * FROM emp e, dept d
WHERE e. deptno = d. deptno
AND e. ename = 'benoit ';
Display the plan using the DBMS_XPLAN.DISPLAY table function
Set linesize 130
Set pagesize 0
SELECT * FROM table (DBMS_XPLAN.DISPLAY );
The output result is as follows:
Plan hash value: 3693697075
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time |
---------------------------------------------------------------------------
| 0 | select statement | 1 | 57 | 6 (34) | 00:00:01 |
| * 1 | hash join | 1 | 57 | 6 (34) | 00:00:01 |
| * 2 | table access full | EMP | 1 | 37 | 3 (34) | 00:00:01 |
| 3 | table access full | DEPT | 4 | 80 | 3 (34) | 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id ):
---------------------------------------------------
1-access ("E". "DEPTNO" = "D". "DEPTNO ")
2-filter ("E". "ENAME" = 'benoit ')
15 rows selected.