运维联盟俱乐部

 找回密码
 立即注册
查看: 1852|回复: 0

[日常管理] q_execution_plan 查询sql_id的执行计划

[复制链接]
  • TA的每日心情
    开心
    2023-8-9 11:05
  • 发表于 2020-9-11 14:40:24 | 显示全部楼层 |阅读模式
    1. set lines 170 pages 1000
    2. select * from table(dbms_xplan.display('&sqlid'));--预估执行计划
    3. select * from table(dbms_xplan.display_cursor('&sqlid'));--内存执行计划
    4. select * from table(dbms_xplan.display_cursor('&sql_id',0,'ADVANCED'));--cursor advanced
    5. select * from table(dbms_xplan.display_awr('&sqlid'));--awr执行计划
    6. select plan_table_output from table (dbms_xplan.display_awr('&sql_id',null,null,'ADVANCED +PEEKED_BINDS'));--awr advanced
    7. select * from table(dbms_xplan.display_sqlset('&sqlid'));--sqlset执行计划
    复制代码

    可以通过下面的三个视图查询到sql执行计划的历史信息:
    DBA_HIST_SQL_PLAN
    DBA_HIST_SQLSTAT
    DBA_HIST_SNAPSHOT
    查看语句的历史执行信息,是否发生变化,何时发生了变化。如果发生了变化,找出以前的执行计划,与当前的执行计划进行对比,有什么不同。

    1. --v$sql_plan
    2. select distinct sql_id,plan_hash_value,to_char(timestamp,'yyyymmdd hh24:mi:ss') timestamp from v$sql_plan where sql_id='&sql_id' order by timestamp;
    复制代码
    1. --dba_hist_sql_plan
    2. select distinct sql_id,plan_hash_value,to_char(timestamp,'yyyymmdd hh24:mi:ss') timestamp from dba_hist_sql_plan where sql_id='&sql_id' order by timestamp;
    复制代码

    1. --v$sql_plan
    2. col options
    3. FOR a15 col operation
    4. FOR a20 col object_name
    5. FOR a20

    6. SET lines 0

    7. SELECT plan_hash_value
    8. ,id
    9. ,operation
    10. ,options
    11. ,object_name
    12. ,depth
    13. ,cost
    14. ,to_char(TIMESTAMP, 'yyyymmdd hh24:mi:ss') TIMESTAMP
    15. FROM v$sql_plan
    16. WHERE sql_id = '&&sql_id'
    17. AND plan_hash_value IN (
    18. SELECT DISTINCT plan_hash_value
    19. FROM v$sql_plan
    20. WHERE sql_id = '&&sql_id'
    21. )
    22. ORDER BY id
    23. ,TIMESTAMP;
    复制代码

    1. --dba_hist_sql_plan
    2. col options
    3. FOR a15 col operation
    4. FOR a20 col object_name
    5. FOR a20

    6. SET lines 0

    7. SELECT plan_hash_value
    8.         ,id
    9.         ,operation
    10.         ,options
    11.         ,object_name
    12.         ,depth
    13.         ,cost
    14.         ,bytes
    15.         ,cpu_cost
    16.         ,io_cost
    17.         ,TIME
    18.         ,to_char(TIMESTAMP, 'yyyymmdd hh24:mi:ss') TIMESTAMP
    19. FROM dba_hist_sql_plan
    20. WHERE sql_id = '&&sql_id'
    21.         AND plan_hash_value IN (
    22.                 SELECT DISTINCT plan_hash_value
    23.                 FROM dba_hist_sql_plan
    24.                 WHERE sql_id = '&&sql_id'
    25.                 )
    26. ORDER BY id
    27.         ,TIMESTAMP;
    复制代码



    查看出来执行计划的变化之后 可以使用如下sql查看发生了那些变化!
    1. col options for a15 col operation for a20 col object_name for a20
    2. select
    3.         plan_hash_value
    4.   , id
    5.   , operation
    6.   , options
    7.   , object_name
    8.   , depth
    9.   , cost
    10.   , to_char(TIMESTAMP,'yyyymmdd hh24:mi:ss')
    11. from
    12.         DBA_HIST_SQL_PLAN
    13. where
    14.         sql_id ='&&sql_id'
    15.         and plan_hash_value in (select distinct(plan_hash_value) from DBA_HIST_SQL_PLAN where sql_id='&&sql_id')
    16. order by ID , TIMESTAMP;
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    运维联盟俱乐部 ( 冀ICP备19036648号 )

    GMT+8, 2024-5-17 11:59 , Processed in 0.045457 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表