查询v$session_longops动态性能视图可以查看运行时间大于6秒的查询,如果想让运行时间大于6秒的查询被数据库引擎收集,必须满足以下条件:
1.参数timed_statistics或SQL_trace为true
2.涉及的对象(表或索引)必须被分析(analyze或dbms_stats)
在v$session_longops视图中,sofar字段表示已经扫描的块数,totalwork表示总得需要扫描的块数,所以我们即可以对正在运行的长查询进行监控,比如在索引创建时,查看索引创建的进度,也可以查看系统中以往的长查询……
col opname format a32
col target_desc format a32
col perwork format a12
set lines 131
select sid,OPNAME,TARGET_DESC,sofar,TOTALWORK,
trunc(sofar/totalwork*100,2)||'%' as perwork
from v$session_longops where sofar!=totalwork;
set lines 121
set pages 999
col opname format a29
col target format a29
col target_desc format a12
col perwork format a12
col remain format 99
col start_time format a21
col sofar format 99999999
col totalwork format 99999999
col SQL_text format a101
col bufgets format 99999999
select opname,target,to_char(start_time,'yy-mm-dd:hh24:mi:ss')
start_time,elapsed_seconds elapsed,
executions execs,buffer_gets/decode(executions,0,1,executions)
bufgets,module,SQL_text
from v$session_longops sl,v$SQLarea sa
where sl.SQL_hash_value = sa.hash_value and upper
(substr(module,1,4)) <>'RMAN' and substr(opname,1,4) <>'RMAN'
and module <>'SQL*Plus' and sl.start_time>trunc(sysdate)