2017-08-24 8 views
3

我有一个需要复杂处理的大型源数据集(几百万行),从而导致数据量大得多,然后应该卸载并存储为文件。存储需要根据特定参数划分结果数据,即符合特定标准的N个源行。部分使用多个pl/sql调用中的游标而未在包规范中对其进行定义

由于可以在PL/SQL中计算所述参数,因此决定最有效的方法是创建一个包,为其中的源行指定一个spec级别的游标,然后编写一个过程部分消耗打开的游标直到符合条件,然后用结果数据填充临时表,然后卸载该过程,然后再次调用该过程,直到没有更多源行。 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不允许绑定SYS_REFCURSOR参数。

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

只有其他的想法至今我已经是做一个专门的包装,存储所述光标,有openclose程序和get_cursor返回REFCURSOR;然后从generator.consume_cur拨打get_cursor。这将使专用包(不太可能改变)有状态和主包无状态。但是,它看起来像是一个半熟的补丁,而不是一个问题的解决方案。有没有更好的方式来实现我所需要的?也许完全改变逻辑而不影响性能和存储限制太多。

+0

另一个想法是:不要经常重新编译它。频繁变化的原因是什么?但是,如果你确实需要,那么[基于版本的重新定义](https://docs.oracle.com/database/121/ADFNS/adfns_editions.htm#ADFNS020)可能会有所帮助。 – krokodilko

+2

我不认为为游标制作专门的包是一个半熟的概念 - 它遵循基本的分离关注点。就像你说的那样,这个包已经做了很多无关的事情 - 听起来像是需要重构。 – kfinity

回答

2

我有一个问题来理解你的问题。但我可以为你的想法提供澄清。

  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

+0

哦,'DBMS_SQL'解决方法很好,从来不知道!与refcursor相比,整数更容易传递。至于JDBC,我知道它支持输出refcursor参数,但我试图将相同的refcursor作为输入参数传递给另一个过程,而JDBC不允许它。 – Timekiller

相关问题