在绝大多数的DBA中,查看oracle sql执行计划的执行顺序:先从最开头一直连续往右看,直到看到最右边的并列的地方;对于不并列的,靠右的先执行;如果见到并列的,就从上往下看,对于并列的部分,靠上的先执行。
实验:
SYS@mzf1> select /*+ xplan_example1 */ t1.empno, t1.ename, t2.dname from scott.emp t1, scott.dept t2 where t1.deptno=t2.deptno;
EMPNO ENAME DNAME
---------- ---------- --------------
7782 CLARK ACCOUNTING
7839 KING ACCOUNTING
7934 MILLER ACCOUNTING
7566 JONES RESEARCH
7902 FORD RESEARCH
7876 ADAMS RESEARCH
7369 SMITH RESEARCH
7788 SCOTT RESEARCH
7521 WARD SALES
7844 TURNER SALES
7499 ALLEN SALES
7900 JAMES SALES
7698 BLAKE SALES
7654 MARTIN SALES
14 rows selected.
SYS@mzf1> select sql_id, sql_text, child_number from v$sql where sql_text like 'select /*+ xplan_example1 */ t1.empno%';
SQL_ID SQL_TEXT CHILD_NUMBER
------------- ---------------------------------------------- ------------
0zgspxd62pbkz select /*+ xplan_example1 */ t1.empno, t1.ename, t2.dname from scott.emp t1, scott.dept t2 where t1.deptno=t2.deptno 0
SYS@mzf1> select * from table(dbms_xplan.display_cursor('0zgspxd62pbkz',0,'advanced'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 0zgspxd62pbkz, child number 0
-------------------------------------
select /*+ xplan_example1 */ t1.empno, t1.ename, t2.dname from
scott.emp t1, scott.dept t2 where t1.deptno=t2.deptno
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 6 (100)| |
| 1 | MERGE JOIN | | 14 | 364 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 182 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / T2@SEL$1
3 - SEL$1 / T2@SEL$1
5 - SEL$1 / T1@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('12.1.0.2')
DB_VERSION('12.1.0.2')
OPT_PARAM('star_transformation_enabled' 'true')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
INDEX(@"SEL$1" "T2"@"SEL$1" ("DEPT"."DEPTNO"))
FULL(@"SEL$1" "T1"@"SEL$1")
LEADING(@"SEL$1" "T2"@"SEL$1" "T1"@"SEL$1")
USE_MERGE(@"SEL$1" "T1"@"SEL$1")
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("T1"."DEPTNO"="T2"."DEPTNO")
filter("T1"."DEPTNO"="T2"."DEPTNO")
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "T2"."DNAME"[VARCHAR2,14], "T1"."EMPNO"[NUMBER,22],
"T1"."ENAME"[VARCHAR2,10]
2 - "T2"."DEPTNO"[NUMBER,22], "T2"."DNAME"[VARCHAR2,14]
3 - "T2".ROWID[ROWID,10], "T2"."DEPTNO"[NUMBER,22]
4 - (#keys=1) "T1"."DEPTNO"[NUMBER,22], "T1"."EMPNO"[NUMBER,22],
"T1"."ENAME"[VARCHAR2,10]
5 - "T1"."EMPNO"[NUMBER,22], "T1"."ENAME"[VARCHAR2,10],
"T1"."DEPTNO"[NUMBER,22]
62 rows selected.
在上面的执行计划中,我们可以根据口诀进行一个顺序的解读:3—2—5—4—1—0,这样的一个顺序进行执行的。
对于简单的SQL语句我们比较好的分析,对于那些复杂的SQL语句我们分析起来是相当的麻烦的。
通过如下的脚本进行分析:
SYS@mzf1> @/home/oracle/xplan.sql
Type created.
Type created.
old 13: &&_awr_start
new 13: /*
old 19: &&_awr_end
new 19: */
Package created.
old 270: &_awr_start
new 270: /*
old 342: &_awr_end
new 342: */
Package body created.
SYS@mzf1> desc xplan
FUNCTION DISPLAY RETURNS XPLAN_NTT
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
P_TABLE_NAME VARCHAR2 IN DEFAULT
P_STATEMENT_ID VARCHAR2 IN DEFAULT
P_FORMAT VARCHAR2 IN DEFAULT
FUNCTION DISPLAY_CURSOR RETURNS XPLAN_NTT
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
P_SQL_ID VARCHAR2 IN DEFAULT
P_CURSOR_CHILD_NO NUMBER(38) IN DEFAULT
P_FORMAT VARCHAR2 IN DEFAULT
执行语句:
SYS@mzf1> select * from table(xplan.display_cursor('0zgspxd62pbkz',0,'advanced'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 0zgspxd62pbkz, child number 0
-------------------------------------
select /*+ xplan_example1 */ t1.empno, t1.ename, t2.dname from
scott.emp t1, scott.dept t2 where t1.deptno=t2.deptno
Plan hash value: 844388907
------------------------------------------------------------------------------------------------
| Id | Order | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------
| 0 | 6 | SELECT STATEMENT | | | | 6 (100)| |
| 1 | 5 | MERGE JOIN | | 14 | 364 | 6 (17)| 00:00:01 |
| 2 | 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | 1 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | 4 | SORT JOIN | | 14 | 182 | 4 (25)| 00:00:01 |
| 5 | 3 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
---------------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / T2@SEL$1
3 - SEL$1 / T2@SEL$1
5 - SEL$1 / T1@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('12.1.0.2')
DB_VERSION('12.1.0.2')
OPT_PARAM('star_transformation_enabled' 'true')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
INDEX(@"SEL$1" "T2"@"SEL$1" ("DEPT"."DEPTNO"))
FULL(@"SEL$1" "T1"@"SEL$1")
LEADING(@"SEL$1" "T2"@"SEL$1" "T1"@"SEL$1")
USE_MERGE(@"SEL$1" "T1"@"SEL$1")
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("T1"."DEPTNO"="T2"."DEPTNO")
filter("T1"."DEPTNO"="T2"."DEPTNO")
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "T2"."DNAME"[VARCHAR2,14], "T1"."EMPNO"[NUMBER,22],
"T1"."ENAME"[VARCHAR2,10]
2 - "T2"."DEPTNO"[NUMBER,22], "T2"."DNAME"[VARCHAR2,14]
3 - "T2".ROWID[ROWID,10], "T2"."DEPTNO"[NUMBER,22]
4 - (#keys=1) "T1"."DEPTNO"[NUMBER,22], "T1"."EMPNO"[NUMBER,22],
"T1"."ENAME"[VARCHAR2,10]
5 - "T1"."EMPNO"[NUMBER,22], "T1"."ENAME"[VARCHAR2,10],
"T1"."DEPTNO"[NUMBER,22]
62 rows selected.
从上述的显示中可以发现多出了一个order列,该列的值从1开始递增,非常直观的告诉我们SQL语句的执行计划的执行顺序。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30192548/viewspace-2129212/
,如需转载,请注明出处,否则将追究法律责任。