如何确定 Oracle 中剩余的 SQL 工作的近似数量?
问题
您想了解长时间运行的 SQL 脚本可能需要多长时间才能完成。
解决方案
我们可以使用 “V$SESSION_LONGOPS" 视图来了解查询执行剩余的估计时间。“V$SESSION_LONGOPS” 视图显示运行时间超过 6 秒的各种数据库操作的状态。请注意,此视图仅提供 SQL 何时可能完成的粗略估计。
示例
select a.username ,a.opname ,b.sql_text ,to_char(a.start_time,'DD-MON-YY HH24:MI') start_time ,a.elapsed_seconds how_long ,a.time_remaining secs_left ,a.sofar ,a.totalwork ,round(a.sofar/a.totalwork*100,2) percent from v$session_longops a ,v$sql b where a.sql_address = b.address and a.sql_hash_value = b.hash_value and a.sofar <> a.totalwork and a.totalwork != 0;
要通过 SQLPLUS 正确查看结果,请首先设置以下参数。
示例
SET LINESIZE 141 TRIMSPOOL ON PAGES 66 COL username FORMAT A8 HEAD "User|Name" COL opname FORMAT A16 HEAD "Operation|Type" COL sql_text FORMAT A33 HEAD "SQL|Text" TRUNC COL start_time FORMAT A15 HEAD "Start|Time" COL how_long FORMAT 99,990 HEAD "Time|Run" COL secs_left FORMAT 99,990 HEAD "Appr.|Secs Left" COL sofar FORMAT 9,999,990 HEAD "Work|Done" COL totalwork FORMAT 9,999,990 HEAD "Total|Work" COL percent FORMAT 999.90 HEAD "%|Done" select a.username ,a.opname ,b.sql_text ,to_char(a.start_time,'DD-MON-YY HH24:MI') start_time ,a.elapsed_seconds how_long ,a.time_remaining secs_left ,a.sofar ,a.totalwork ,round(a.sofar/a.totalwork*100,2) percent from v$session_longops a ,v$sql b where a.sql_address = b.address and a.sql_hash_value = b.hash_value? and a.sofar <> a.totalwork and a.totalwork != 0;