|
本系列文章导航
[Oracle]高效的PL/SQL程序设计(一)--伪列ROWNUM使用技巧
[Oracle]高效的PL/SQL程序设计(二)--标量子查询
[Oracle]高效的PL/SQL程序设计(三)--Package的优点
[Oracle]高效的PL/SQL程序设计(四)--批量处理
[Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
[Oracle]高效的PL/SQL程序设计(六)--%ROWTYPE的使用
批量处理一般用在ETL操作, ETL代表提取(extract),转换(transform),装载(load), 是一个数据仓库的词汇!
类似于下面的结构:
forx(select*from...)
loop
Processdata;
insertintotablevalues(...);
endloop;
一般情况下, 我们处理大笔的数据插入动作, 有2种做法, 第一种就是一笔笔的循环插入
createtablet1asselect*fromuser_tableswhere1=0;
createtablet2asselect*fromuser_tableswhere1=0;
createtablet3asselecttable_namefromuser_tableswhere1=0;
createorreplaceprocedureNor_Test
as
begin
forxin(select*fromuser_tables)
loop
insertintot1valuesx;
endloop;
end;
第2种方法就是批量处理(insert全部字段):
createorreplaceprocedureBulk_Test1(p_array_sizeinnumber)
as
typearrayistableofuser_tables%rowtype;
l_dataarray;
cursorcisselect*fromuser_tables;
begin
openc;
loop
fetchcbulkcollectintol_datalimitp_array_size;
foralliin1..l_data.count
insertintot2valuesl_data(i);
exitwhenc%notfound;
endloop;
end;
insert部分字段:
createorreplaceprocedureBulk_Test2(p_array_sizeinnumber)
as
l_tablenamedbms_sql.Varchar2_Table;
cursorcisselecttable_namefromuser_tables;
begin
openc;
loop
fetchcbulkcollectintol_tablenamelimitp_array_size;
foralliin1..l_tablename.count
insertintot3values(l_tablename(i));
exitwhenc%notfound;
endloop;
end;
在性能方面批量处理有着很大的优势, p_array_size一般默认都是100
博文来源:http://blog.csdn.net/huanghui22/archive/2007/05/22/1621290.aspx |
|