oracle 可以从数据库中一次检索出一条数据,也可以一次检索一批记录,或者说检索一组记录。 批量提取是指一次单独的提取操作中从oracle检索多条记录的机制,批量提取检索记录可以减少发送到数据库服务器的请求次数,也可以降低网络流量和逻辑io的开销。
如下2个例子实现批量提取与插入操作
declare
type recstartyp is table of emp%rowtype index by BINARY_INTEGER;
rec_tab recstartyp;
cursor temp is select * from emp;
begin
open temp;
fetch temp bulk collect into rec_tab; --批量提取所有记录
FORALL i in rec_tab.first..rec_tab.last
insert into emp1 values rec_tab(i);
commit;
close temp;
end;
/
declare
type tntbl_array is table of table_source%rowtype;
vntbl_array tntbl_array;
cursor cur_source is select * from table_source;
i_arraysize integer := 100;
err_num number;
err_msg varchar2(1000);
begin
open cur_source;
loop
fetch cur_source
bulk collect
into vntbl_array
limit i_arraysize; --每次批量提取一部分记录 100条(limit字句用来设置批量提取的数量)
forall i in 1..vntbl_array.count --批量插入
insert into table_target
values vntbl_array(i);
--2万条提交一次
if ( mod(cur_source%rowcount, 20000) = 0) then
commit;
end if;
exit when cur_source%notfound;
end loop;
commit;
if ( cur_source%isopen ) then
close cur_source;
end if;
end;