如何查看oracle执行计划:查看已知sql_id的真实执行计划

有时需要抓取正在运行的SQL的执行计划,这时我们需要获取 SQL的SQL_ID以及SQL的CHILD_NUMEBR, 然后将其代入下面SQL, 就能获取正在运行的SQL的执行计划。

select * from table(dbms_xplan.display_cursor('sql_id',child_number));

示例:

Create table a as select * from dba_objects;
Create table b as select * from dba_objects;
SQL> select count(*) from a,b where a.owner=b.owner;

然后再在另外一个会话跑如下命令

select a.sid, a.event, a.sql_id, a.sql_child_number, b.sql_text
from v$session a, v$sql b 
where a.sql_address = b.address 
and a.sql_hash_value = b.hash_value and a.sql_child_number = b.child_number order by 1 desc; 

这个是已知SQLID去找sql_child_number

select sid,SERIAL#,event,sql_id,sql_child_number from gv$session where sql_id='czr9jwxv0xra6';

如果被刷掉了,可以通过这个查

select event,sql_id,sql_child_number from dba_hist_active_sess_history where sql_id='czr9jwxv0xra6';

在这里插入图片描述

select * from table(dbms_xplan.display_cursor('czr9jwxv0xra6',0));

如何查看oracle执行计划:查看已知sql_id的真实执行计划_第1张图片

也可以用下面脚本去抓当前正在跑的SQL语句:

select (sysdate-a.logon_time)*24*60 minutes,
a.username,
a.BLOCKING_INSTANCE,
a.BLOCKING_SESSION,
a.program,
a.machine,
a.osuser,
a.status,
a.sid,
a.serial#,
a.event,
a.p1,
a.p2,
a.p3,
a.sql_id,
a.sql_child_number,
b.sql_text from v$session a, v$sql b 
where
a.sql_address = b.address and a.sql_hash_value = b.hash_value and a.sql_child_number=b.child_number and a.username like '%USERNAME%' order by 1 desc;

你可能感兴趣的:(日常运维,oracle,sql,数据库,dba,运维,数据库开发)