文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

oracle查看执行计划之DBMS_XPLAN

2024-04-02 19:55

关注

    使用DBMS_XPLAN包中的方法是在oracle数据库中得到目标SQL的执行计划的另一种方法。针对不同的应用场景吗,你可以选择如下四种方法中的一种:

    select * from table(dbms_xplan.display)

    select * from table(dbms_xplan.display_cursor(null,null,'advenced'));

    select * from table(dbms_xplan.display_cursor('sql_id/hash_value',child_cursor_number,'advanced'));

    select * from table(dbms_xplan.display_awr('sql_id'));

    方法1是执行select * from table(dbms_xplan.display),这需要与explain plan命令配合使用,它用于查看使用explain plan命令后得到的执行计划。

     方法2是执行select * from table(dbms_xplan.display_cursor(null,null,'advenced'));它用于查看刚刚执行过的sql的执行计划,这里针对dbms_xplan.display_cursor所传入的第一个和第二参数的值均为null,第三参数的值是“advanced”,第三个输入参数的值也可以是“all”,只不过用“advanced”后的显示结果会比用“all”的显示结果更详细些。    

SQL> conn scott/scott;

Connected.

SQL> set linesize 800

SQL> set pagesize 900

SQL> col plan_table_output for a200

SQL> select empno,ename,dname from emp,dept where emp.deptno=dept.deptno;

     EMPNO ENAME      DNAME

---------- ---------- --------------

      7782 CLARK      ACCOUNTING

      7839 KING       ACCOUNTING14 rows selected.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));

PLAN_TABLE_OUTPUT

-------------------------------------------------------------------------------------------

SQL_ID 7ww0fhpbqqt0n, child number 0

-------------------------------------

select empno,ename,dname from emp,dept where emp.deptno=dept.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 / DEPT@SEL$1

   3 - SEL$1 / DEPT@SEL$1

   5 - SEL$1 / EMP@SEL$1

Outline Data

-------------

 

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")

       filter("EMP"."DEPTNO"="DEPT"."DEPTNO")

Column Projection Information (identified by operation id):

-----------------------------------------------------------

   1 - "DNAME"[VARCHAR2,14], "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10]

   2 - "DEPT"."DEPTNO"[NUMBER,22], "DNAME"[VARCHAR2,14]

   3 - "DEPT".ROWID[ROWID,10], "DEPT"."DEPTNO"[NUMBER,22]

   4 - (#keys=1) "EMP"."DEPTNO"[NUMBER,22], "EMPNO"[NUMBER,22],

       "ENAME"[VARCHAR2,10]

   5 - "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10], "EMP"."DEPTNO"[NUMBER,22]

58 rows selected.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'all'));

PLAN_TABLE_OUTPUT

-------------------------------------------------------------------------------------------

SQL_ID 7ww0fhpbqqt0n, child number 0

-------------------------------------

select empno,ename,dname from emp,dept where emp.deptno=dept.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 / DEPT@SEL$1

   3 - SEL$1 / DEPT@SEL$1

   5 - SEL$1 / EMP@SEL$1

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")

       filter("EMP"."DEPTNO"="DEPT"."DEPTNO")

Column Projection Information (identified by operation id):

-----------------------------------------------------------

   1 - "DNAME"[VARCHAR2,14], "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10]

   2 - "DEPT"."DEPTNO"[NUMBER,22], "DNAME"[VARCHAR2,14]

   3 - "DEPT".ROWID[ROWID,10], "DEPT"."DEPTNO"[NUMBER,22]

   4 - (#keys=1) "EMP"."DEPTNO"[NUMBER,22], "EMPNO"[NUMBER,22],

       "ENAME"[VARCHAR2,10]

   5 - "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10], "EMP"."DEPTNO"[NUMBER,22]

41 rows selected.

当参数传入all,显示结果中了“Qutline Data”部分的内容:

    方法3是执行    select * from table(dbms_xplan.display_cursor('sql_id/hash_value',child_cursor_number,'advanced'));它用于查看指定sql的执行计划。这里针对方法dbms_xplan.display_cursor所传人的第一个参数的值是指定sql的sql_id或者sql hash value,第二参数的值是要看执行计划所在的child cursor number,第三个参数是advcanced或者all

    

SQL> select sql_text,sql_id,hash_value,child_number from v$sql where sql_text like 'select empno,ename,dname%';

SQL_TEXT

select empno,ename,dname from emp,dept where emp.deptno=dept.deptno

SQL_ID      HASH_VALUE CHILD_NUMBER

------------- ---------- ------------

7ww0fhpbqqt0n 1466655764    0

    只要目标sql的执行计划所在的child cursor还没有被page out出shared pool,就可以使用方法3查看该sql的执行计划:   

SQL> select * from table(dbms_xplan.display_cursor('2qm0f3qgsqqyc',0,'advanced'));

PLAN_TABLE_OUTPUT

-------------------------------------------------------------------------------------------

SQL_ID 2qm0f3qgsqqyc, child number 0

-------------------------------------

select empno,ename,dname from scott.emp,scott.dept where

scott.emp.deptno=scott.dept.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 / DEPT@SEL$1

   3 - SEL$1 / DEPT@SEL$1

   5 - SEL$1 / EMP@SEL$1

Outline Data

-------------

 

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")

       filter("EMP"."DEPTNO"="DEPT"."DEPTNO")

Column Projection Information (identified by operation id):

-----------------------------------------------------------

   1 - "DNAME"[VARCHAR2,14], "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10]

   2 - "DEPT"."DEPTNO"[NUMBER,22], "DNAME"[VARCHAR2,14]

   3 - "DEPT".ROWID[ROWID,10], "DEPT"."DEPTNO"[NUMBER,22]

   4 - (#keys=1) "EMP"."DEPTNO"[NUMBER,22], "EMPNO"[NUMBER,22],

       "ENAME"[VARCHAR2,10]

   5 - "EMPNO"[NUMBER,22], "ENAME"[VARCHAR2,10], "EMP"."DEPTNO"[NUMBER,22]

59 rows selected.

    我们可以通过dbms_xplan.display_awr来得到该sql的所有历史执行计划:

SQL> set lines 100

SQL> select * from table(dbms_xplan.display_awr('cy097a90nu4fk'));

PLAN_TABLE_OUTPUT

SQL_ID cy097a90nu4fk

--------------------

Plan hash value: 1999179007

-------------------------------------------------------------------------------------------

| Id  | Operation                        | Name           | Rows  | Bytes | Cost (%CPU)| Time     |

Inst   |IN-OUT|

-------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT                 |                |       |       |    11 (100)|          |

       |      |

|   1 |  HASH GROUP BY                   |                |     1 |   101 |    11  (19)| 00:00:01 |

       |      |

|   2 |   VIEW                           |                |     1 |   101 |    10  (10)| 00:00:01 |

       |      |

|   3 |    HASH GROUP BY                 |                |     1 |   261 |    10  (10)| 00:00:01 |

       |      |

|   4 |     FILTER                       |                |       |       |            |          |

       |      |

|   5 |      NESTED LOOPS                |                |       |       |            |          |

       |      |

|   6 |       NESTED LOOPS               |                |     1 |   261 |     9   (0)| 00:00:01 |

       |      |

|   7 |        REMOTE                    |                |     1 |   176 |     5   (0)| 00:00:01 |

DBLK_~ | R->S |

|   8 |        INDEX RANGE SCAN          | PRODUCTSPEC_PK |     1 |       |     1   (0)| 00:00:01 |

       |      |

|   9 |       TABLE ACCESS BY INDEX ROWID| PRODUCTSPEC    |     1 |    41 |     2   (0)| 00:00:01 |

       |      |

-------------------------------------------------------------------------------------------

Remote SQL Information (identified by operation id):

----------------------------------------------------

   7 - SELECT "A1"."EVENTTIMEKEY","A1"."EVENTID","A1"."EVENTTIME","A1"."FOLLOTID","A1"."PRODUCTSPECI

D","A1"

       ."PROCESSFLOWID","A1"."OLDOPERATIONID","A1"."QUANTITY","A1"."EQUIPMENTID","A2"."PROCESSFLOWID

","A2"."REWORK

       TYPE" FROM "LOTHISTORY" "A1","PROCESSFLOW" "A2" WHERE "A2"."REWORKTYPE"='Normal' AND

       "A1"."PROCESSFLOWID"="A2"."PROCESSFLOWID" AND "A1"."EVENTID"='TrackIn' AND "A1"."OLDOPERATION

ID"='BFOP001'

       AND (SUBSTR("A1"."FOLLOTID",1,1)='V' OR SUBSTR("A1"."FOLLOTID",1,1)='P') AND

       "A1"."EVENTTIME">=TO_DATE('20140918080001','YYYY-MM-DD HH24:MI:SS') AND

       "A1"."EVENTTIME"<=TO_DATE('20140919080000','YYYY-MM-DD HH24:MI:SS') (accessing 'DBLK_RPT_EOL_

PRD' )

55 rows selected.

    不过此方法显示执行计划中看不到执行步骤对应的谓词条件。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯