【问题标题】:Partially consuming a cursor in multiple pl/sql calls without defining it in package spec在多个 pl/sql 调用中部分使用游标而不在包规范中定义它
【发布时间】:2018-02-02 11:47:07
【问题描述】:

我有一个需要复杂处理的大型源数据集(几百万行),导致数据量大得多,然后应该将其卸载并存储为文件。存储需要根据特定参数划分结果数据,即满足特定标准的N个源行。

由于可以在 PL/SQL 中计算上述参数,因此决定最有效的方法是创建一个包,为其中的源行指定规范级别的游标,然后编写一个过程使用打开的游标直到满足条件并用结果数据填充临时表,然后将其卸载,并且将再次调用该过程,重复直到没有更多的源行。 PL/SQL 基本上是这样的:

create or replace PACKAGE BODY generator as

  cursor glob_cur_body(cNo number) is
    select *
      from source_table
     where no = cNo
     order by conditions;

  procedure close_cur is
  begin
    if glob_cur_body%isopen then
      close glob_cur_body;
    end if;
  end close_cur;

  procedure open_cur(pNo number) is
  begin
    close_cur; 
    open glob_cur_body(pNo);
  end open_cur;

  function consume_cur return varchar2 is
    v source_table%rowtype;
    part_id varchar2(100);
  begin
    fetch glob_cur_body into v;
    if glob_cur_body%notfound then
      return null;
    end if;
    --Clear temporary tables
    --Do the processing until criteria is meet of there's no more rows
    --Fill the temporary tables and part_id
    return part_id;
  end consume_cur;
end generator;

消费者正在执行以下操作(在伪代码中)

generator.open_cur;
part_id = generator.consume;
while ( part_id != null )
{
//offload data from temp tables
part_id = generator.consume;
}
generator.close_cur;

它工作正常,但不幸的是有一个问题:规范级别的游标使包有状态,这意味着它的重新编译导致之前已经访问过它的会话ORA-04068。它使维护变得很麻烦,因为除了上述功能之外,该软件包还有很多其他功能,而且它被积极用于不相关的目的。

所以,我想摆脱规范级别的光标,但我不确定这是否可能。我已经放弃了一些想法:

  • 重新打开游标并跳过 N 行:性能糟糕,不可靠,因为在打开之间对数据所做的任何更改都会影响

  • 将源游标提取到 plsql 表中:大小太大。

  • 一次填满整个卸载表,稍后再拆分:大小太大,性能不佳。

  • 将游标作为 refcursor 打开并将 refcursor 变量存储在专用包中:不可能,因为 pl/sql 不允许在规范级别使用 sys_refcursor 变量

  • open_cur 过程返回 refcursor,将其存储在卸载程序中,然后以某种方式将其传递给 consume_cur:看起来可行,但卸载程序使用 Java,并且 JDBC 不允许绑定 @987654327 @参数。

  • consume_cur 更改为流水线函数:本来可以,但是oracle 会缓冲流水线行,这意味着它会在逐行获取数据时执行多次。也违反直觉。

到目前为止,我唯一的另一个想法是制作一个存储所述游标的专用包,具有 openclose 过程和 get_cursor 返回 refcursor;然后从generator.consume_cur 拨打get_cursor。这将使专用包(不太可能更改)有状态,而主包无状态。然而,这似乎是一个半生不熟的补丁,而不是一个问题的解决方案。有没有更体面的方式来实现我所需要的?也许完全改变逻辑而不会过多影响性能和存储限制。

【问题讨论】:

  • 另一个想法是:不要经常重新编译它。变化如此频繁的原因是什么?但如果你真的需要,那么Edition Based Redefinition 可能会有所帮助。
  • 我不认为为光标制作一个专用包是一个半生不熟的想法 - 它遵循基本的关注点分离。就像你说的,这个包已经做了很多不相关的事情——听起来它需要重构。

标签: oracle stored-procedures plsql cursor


【解决方案1】:

我很难理解你的问题。但我可以澄清你的想法。

  1. 将光标作为 refcursor 打开并将 refcursor 变量存储在 专用包:不可能,因为 pl/sql 不允许 sys_refcursor 规格级别的变量

dbms_sql 的解决方法。

create table test_rows as  (select level rr from dual connect by level <= 100);

create or replace package cursor_ctx is 
 ctx_number integer;
end;  


declare 
 p_cursor sys_refcursor;
begin
 open p_cursor for 'select rr from test_rows'; 
  cursor_ctx.ctx_number := DBMS_SQL.TO_CURSOR_NUMBER(p_cursor);
end;

这部分消耗的是来自游标的数据。

declare 
 p_cursor sys_refcursor;
 type l_number is table of number;
 v_numbers l_number;
begin
  if  DBMS_SQL.IS_OPEN(cursor_ctx.ctx_number) then
    p_cursor := DBMS_SQL.TO_REFCURSOR(  cursor_ctx.ctx_number);
    fetch p_cursor bulk collect into v_numbers limit 10;
        if v_numbers.count < 10 then 
            dbms_output.put_line('No more data, close cursor');
            close p_cursor;
            cursor_ctx.ctx_number := null;
        else 
            cursor_ctx.ctx_number := DBMS_SQL.TO_CURSOR_NUMBER(p_cursor);
        end if;
        for i in nvl(v_numbers.first,1) .. nvl(v_numbers.last,-1) loop
            dbms_output.put_line(v_numbers(i));
           end loop;
    else 
     dbms_output.put_line('Null or cursor close ');
    end if;    
end;
  1. 流水线函数未来可将输入光标拆分为块。 Parallel Enabled Pipelined Table Functions

  2. JDBC 允许使用 sys_refcursor 作为输出参数。 sys_refcursor = ResultSet

【讨论】:

  • 哦,DBMS_SQL 解决方法很好,从来不知道!与 refcursor 相比,整数更容易传递。至于 JDBC,我知道它支持输出 refcursor 参数,但我试图将相同的 refcursor 作为输入参数传递回另一个过程,而 JDBC 不允许这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多