您的位置:首页 > 数据库 > Oracle

Oracle 中使用fetch bulk collect into 批量效率的读取游标数据

2015-11-22 21:57 726 查看
fetchbulkcollectinto的使用格式是:fetchsome_cursorcollectintocol1,col2limitxxx。col1、col2是声明的集合类型变量,xxx为每次取数据块的大小(记录数),相当于缓冲区的大小,可以不指定limitxxx大小。下面以实际的例子来说明它的使用,并与逐条取记录的fetchinto执行效率上进行比较。

使用fetchbulkcollectinto获取游标数据

01.
declare

02.

03.
--声明需要集合类型及变量,参照字段的type来声明类型

04.
typeid_type
is
table
of
sr_contacts.sr_contact_id%type;

05.
v_idid_type;

06.

07.
typephone_type
is
table
of
sr_contacts.contact_phone%type;

08.
v_phonephone_type;

09.

10.
typeremark_type
is
table
of
sr_contacts.remark%type;

11.
v_remarkremark_type;

12.

13.
cursor
all_contacts_cur
is
--用
rownum来限定取出的记录数来测试

14.
select
sr_contact_id,contact_phone,remark
from
sr_contacts
where
rownum
<=100000;

15.

16.
begin

17.

18.
open
all_contacts_cur;

19.
loop

20.
fetch
all_contacts_curbulkcollect
into
v_id,v_phone,v_remark
limit256;

21.
for
i
in
1..v_id.
count
loop
--遍历集合

22.
--用v_id(i)/v_phone(i)/v_remark(i)取出字段值来执行你的业务逻辑

23.
null
;
--这里只放置一个空操作,只为测试循环取数的效率

24.
end
loop;

25.
exit
when
all_contacts_cur%notfound;
--exit
不能紧接fetch了,不然会漏记录

26.
end
loop;

27.
close
all_contacts_cur;

28.
end
;


使用fetchinto逐行获取游标数据



01.
declare

02.

03.
--声明变量,参照字段的type来声明类型

04.
v_idsr_contacts.sr_contact_id%type;

05.
v_phonesr_contacts.contact_phone%type;

06.
v_remarksr_contacts.remark%type;

07.

08.
cursor
all_contacts_cur
is
--用
rownum来限定取出的记录数来测试

09.
select
sr_contact_id,contact_phone,remark
from
sr_contacts
where
rownum
<=100000;

10.

11.
begin

12.

13.
open
all_contacts_cur;

14.
loop

15.
fetch
all_contacts_cur
into
v_id,v_phone,v_remark;

16.
exit
when
all_contacts_cur%notfound;

17.
--用v_id/v_phone/v_remark取出字段值来执行你的业务逻辑

18.
null
;
--这里只放置一个空操作,只为测试循环取数的效率

19.
end
loop;

20.
close
all_contacts_cur;

21.
end
;


执行性能比较

看看测试的结果,分别执行五次所耗费的秒数:

当rownum<=100000时:

fetchbulkcollectinto耗时:0.125秒,0.125秒,0.125秒,0.125秒,0.141秒

fetchinto耗时:1.266秒,1.250秒,1.250秒,1.250秒,1.250秒

当rownum<=1000000时:

fetchbulkcollectinto耗时:1.157秒,1.157秒,1.156秒,1.156秒,1.171秒

fetchinto耗时:12.128秒,12.125秒,12.125秒,12.109秒,12.141秒

当rownum<=10000时:

fetchbulkcollectinto耗时:0.031秒,0.031秒,0.016秒,0.015秒,0.015秒

fetchinto耗时:0.141秒,0.140秒,0.125秒,0.141秒,0.125秒

当rownum<=1000时:

fetchbulkcollectinto耗时:0.016秒,0.015秒,0.016秒,0.016秒,0.015秒

fetchinto耗时:0.016秒,0.031秒,0.031秒,0.032秒,0.015秒

从测试结果来看游标的记录数越大时,用fetchbulkcollectinto的效率很明显示,趋于很小时就差不多了。

注意了没有,前面使用fetchbulkcollectinto时前为每一个查询列都定义了一个集合,这样有些繁琐。我们之前也许用过表的%rowtype类型,同样的我们也可以定义表的%rowtype的集合类型。看下面的例子,同时在这个例子中,我们借助于集合的first、last属性来代替使用count属性来进行遍历。

01.
declare

02.

03.
--声明需要集合类型及变量,参照字段的type来声明类型

04.
typecontacts_type
is
table
of
sr_contacts%rowtype;

05.
v_contactscontacts_type;

06.

07.
cursor
all_contacts_cur
is
--用
rownum来限定取出的记录数来测试

08.
select
*
from
sr_contacts
where
rownum
<=10000;

09.

10.
begin

11.

12.
open
all_contacts_cur;

13.
loop

14.
fetch
all_contacts_curbulkcollect
into
v_contacts
limit256;

15.
for
i
in
v_contacts.
first
..
v_contacts.
last
loop
--遍历集合

16.
--用v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark

17.
--的形式来取出各字段值来执行你的业务逻辑

18.
null
;
--这里只放置一个空操作,只为测试循环取数的效率

19.
end
loop;

20.
exit
when
all_contacts_cur%notfound;

21.
end
loop;

22.
close
all_contacts_cur;

23.
end
;


关于limit参数



你可以根据你的实际来调整limit参数的大小,来达到你最优性能。limit参数会影响到pga的使用率。而且也可以在fetchbulk中省略limit参数,写成

fetchall_contacts_curbulkcollectintov_contacts;

有些资料中是说,如果不写limit参数,将会以数据库的arraysize参数值作为默认值。在sqlplus中用showarraysize可以看到该值默认为15,setarraysize256可以更改该值。而实际上我测试不带limit参数时,外层循环只执行了一轮,好像不是limit15,所以不写limit参数时,可以去除外层循环,begin-end部分可写成:

01.
begin

02.
open
all_contacts_cur;

03.
fetch
all_contacts_curbulkcollect
into
v_contacts;

04.
for
i
in
v_contacts.
first
..
v_contacts.
last
loop
--遍历集合

05.
--用v_contacts(i).sr_contact_id/v_contacts(i).contact_phone/v_contacts(i).remark

06.
--的形式来取出各字段值来执行你的业务逻辑

07.
null
;
--这里只放置一个空操作,只为测试循环取数的效率

08.
dbms_output.put_line(2000);

09.
end
loop;

10.
close
all_contacts_cur;

11.
end
;


bulkcollect的其他用法(总是针对集合)

selectinto语句中,如:

SELECTsr_contact_id,contact_phoneBULKCOLLECTINTOv_id,v_phone

FROMsr_contactsWHEREROWNUM<=100;

dbms_output.put_line('Count:'||v_id.count||',First:'||v_id(1)||'|'||v_phone(1));

returninginto语句中,如:

DELETEFROMsr_contactsWHEREsr_contact_id<30

RETURNINGsr_contact_id,contact_phoneBULKCOLLECTINTOv_id,v_phone;

dbms_output.put_line('Count:'||v_id.count||',First:'||v_id(1)||'|'||v_phone(1));

forall的bulkdml操作,它大大优于for集合后的操作

fetchall_contacts_curbulkcollectintov_contacts;

foralliin1..v_contacts.count

--foralliinv_contacts.first..v_contacts.last

--foralliinindicesofv_contacts--10g以上,可以是非连续的集合

insertintosr_contacts(sr_contact_id,contact_phone,remark)

values(v_contacts(i).sr_contact_id,v_contacts(i).contact_phone,v_contacts(i).remark);

--或者是单条的delete/update操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: