有没有办法将一个PL / SQL数组变量的内容批量复制到另一个?

时间:2013-09-09 13:53:35

标签: oracle plsql

假设我的包中有2个相同类型的表变量。请注意,这些类型是包的本地类型。

TYPE MyTableType is table of some_table%ROWTYPE;
table1 MyTableType;
table2 MyTableType;

我在程序包中有一个过程,它将一些数据加载到table1和table2中。在这之后的某个时刻,我想将table1中当前的所有内容添加到table2中。

有没有比循环table1和.extend - table2更好的方法,然后在每次迭代时设置.last元素的值?

我正在寻找某种快速的批量操作(如果存在的话)。是吗?

1 个答案:

答案 0 :(得分:6)

22:02:22 SYSTEM@dwal> ed
Wrote file S:\spool\dwal\BUFFER_SYSTEM_386.sql

  1   declare
  2     type t is table of dual%rowtype;
  3     c1 t := t();
  4     c2 t := t();
  5   begin
  6     c1.extend;
  7     c1(1).dummy := 'a';
  8     c2.extend(2);
  9     c2(1).dummy := 'b';
 10     c2(2).dummy := 'c';
 11     c2 := c1 multiset union all c2;
 12     for i in c2.first .. c2.last loop
 13       dbms_output.put_line(c2(i).dummy);
 14     end loop;
 15*  end;
22:02:41 SYSTEM@dwal> /
a
b
c

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.26

upd:我从来没有机会对多线程操作进行基准测试,aaaaaand,尽管它们看起来实际上很慢,但实际上还是很慢:

22:14:56 SYSTEM@dwal> ed
Wrote file S:\spool\dwal\BUFFER_SYSTEM_331.sql

  1  declare
  2    cnt int := 1e5;
  3    type t is table of dual%rowtype;
  4    c1 t := t();
  5    c2 t := t();
  6    timer int;
  7    procedure prebuild as
  8    begin
  9      c1.delete;
 10      c2.delete;
 11      c1.extend(cnt);
 12      c2.extend(cnt);
 13      for i in 1 .. cnt loop
 14        c1(i).dummy := dbms_random.string('l', 1);
 15        c2(i).dummy := dbms_random.string('l', 1);
 16      end loop;
 17    end;
 18  begin
 19    -- 1
 20    prebuild;
 21    timer := dbms_utility.get_cpu_time;
 22    for i in 1 .. cnt loop
 23      c2.extend;
 24      c2(c2.last) := c1(i);
 25    end loop;
 26    dbms_output.put_line(dbms_utility.get_cpu_time - timer);
 27    -- 2
 28    prebuild;
 29    timer := dbms_utility.get_cpu_time;
 30    c2 := c2 multiset union all c1;
 31    dbms_output.put_line(dbms_utility.get_cpu_time - timer);
 32    -- 3
 33    prebuild;
 34    timer := dbms_utility.get_cpu_time;
 35    c2.extend(c1.count);
 36    for i in 1 .. cnt loop
 37      c2(c2.count - c1.count + i) := c1(i);
 38    end loop;
 39    dbms_output.put_line(dbms_utility.get_cpu_time - timer);
 40* end;
22:15:00 SYSTEM@dwal> /
15
25
10

PL/SQL procedure successfully completed.

Elapsed: 00:00:03.38

这些数字相当稳定。第三种方法,只延伸第二次收集,然后附加到它,似乎是最快的。