监视长时间运行的PL/SQL块

Jam*_*mes 6 sql oracle plsql sqlplus dbms-output

我有一个相当耗时的PL/SQL块,可以从分子结构中构建指纹.我想将输出打印到SQL*Plus控制台,以提供有关已处理的结构数量的反馈.我可以这样做dbms_output.put_line

但是,每次调用新行时都会写入.我想覆盖这条线.

例如,目前我有以下内容.

Structure x of y processed
Structure x of y processed
Structure x of y processed
Structure x of y processed
Run Code Online (Sandbox Code Playgroud)

最后,当我处理成千上万的结构记录时,我填满了缓冲区.

有没有一种方法可以用来覆盖最后一条输出线?

Jus*_*ave 15

使用DBMS_OUTPUT意味着SQL*Plus将在整个PL/SQL块完成之前不显示任何内容,然后将显示当前缓冲区中的所有数据.因此,它不是提供持续状态的适当方式.

另一方面,Oracle确实提供了一个DBMS_APPLICATION_INFO包,专门用于帮助您监视正在运行的代码.例如,你可以做类似的事情

CREATE PROCEDURE process_structures
AS
  <<other variable declarations>>

  rindex    BINARY_INTEGER;
  slno      BINARY_INTEGER;
  totalwork NUMBER := y; -- Total number of structures
  worksofar NUMBER := 0; -- Number of structures processed
BEGIN
  rindex := dbms_application_info.set_session_longops_nohint;

  FOR i IN (<<select structures to process>>)
  LOOP
    worksofar := worksofar + 1;
    dbms_application_info.set_session_longops(
        rindex      => rindex, 
        slno        => slno,
        op_name     => 'Processing of Molecular Structures', 
        sofar       => worksofar , 
        totalwork   => totalwork, 
        target_desc => 'Some description',
        units       => 'structures');
    <<process your structure with your existing code>>
  END LOOP;
END;
Run Code Online (Sandbox Code Playgroud)

从单独的SQL*Plus会话中,您可以通过查询V$SESSION_LONGOPS视图来监控进度

SELECT opname,
       target_desc,
       sofar,
       totalwork,
       units,
       elapsed_seconds,
       time_remaining
  FROM v$session_longops
 WHERE opname = 'Processing of Molecular Structures';
Run Code Online (Sandbox Code Playgroud)