create table

-- drop table tmp_20190706_220000
-- truncate table tmp_20190706_220000

create table tmp_20190706_220000 (
id integer,
name varchar(100),
memo varchar(200)
)
;

1
2
3
4
5
6
7
8
9
10
11
plsql into

通常简单的写法如下

declare
lv_id tmp_20190706_220000.id%type;
lv_name tmp_20190706_220000.name%type;
lv_memo tmp_20190706_220000.memo%type;

lv_sql varchar2(4000);
cur_tmp sys_refcursor;

begin
lv_sql:='select id,name,memo from tmp_20190704_220000 ';
open cur_tmp for lv_sql;
loop
fetch cur_tmp into lv_id,lv_name,lv_memo ;

exit when cur_tmp%notfound;

end loop;

close cur_tmp;

if cur_tmp%isopen then
close cur_tmp;
end if;

end;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
消耗时长

plsql bulk collect into

bulk collect 的写法如下

declare
type type_tmp_id is table of tmp_20190706_220000.id%TYPE index by pls_integer;
type type_tmp_name is table of tmp_20190706_220000.name%TYPE index by pls_integer;
type type_tmp_memo is table of tmp_20190706_220000.memo%TYPE index by pls_integer;

lv_id type_tmp_id;
lv_name type_tmp_name;
lv_memo type_tmp_memo;

lv_sql varchar2(4000);
cur_tmp sys_refcursor;

CN_BATCH_SIZE constant pls_integer :=100000;

begin
lv_sql:='select id,name,memo from tmp_20190704_220000 ';
open cur_tmp for lv_sql;
loop
fetch cur_tmp bulk collect into lv_id,lv_name,lv_memo limit CN_BATCH_SIZE;

exit when lv_id.count < CN_BATCH_SIZE;

end loop;

close cur_tmp;

if cur_tmp%isopen then
close cur_tmp;
end if;

end;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
消耗时长

差异还是蛮大的。

plsql into & insert into

declare
lv_id tmp_20190706_220000.id%type;
lv_name tmp_20190706_220000.name%type;
lv_memo tmp_20190706_220000.memo%type;

lv_sql varchar2(4000);
cur_tmp sys_refcursor;

begin
lv_sql:='select id,name,memo from tmp_20190704_220000 ';
open cur_tmp for lv_sql;
loop
fetch cur_tmp into lv_id,lv_name,lv_memo ;
insert into tmp_20190706_220000
( id,
name,
memo
)
values(
lv_id,
lv_name,
lv_memo
)
;
exit when cur_tmp%notfound;

end loop;

commit;
close cur_tmp;

if cur_tmp%isopen then
close cur_tmp;
end if;

end;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
消耗时长

plsql bulk collect into & insert into

declare
type type_tmp_id is table of tmp_20190706_220000.id%TYPE index by pls_integer;
type type_tmp_name is table of tmp_20190706_220000.name%TYPE index by pls_integer;
type type_tmp_memo is table of tmp_20190706_220000.memo%TYPE index by pls_integer;

lv_id type_tmp_id;
lv_name type_tmp_name;
lv_memo type_tmp_memo;

lv_sql varchar2(4000);
cur_tmp sys_refcursor;

CN_BATCH_SIZE constant pls_integer :=100000;

begin
lv_sql:='select id,name,memo from tmp_20190704_220000 ';
open cur_tmp for lv_sql;
loop
fetch cur_tmp bulk collect into lv_id,lv_name,lv_memo limit CN_BATCH_SIZE;
forall c_k in lv_id.FIRST .. lv_id.LAST
insert into tmp_20190706_220000(
id,
name,
memo
)
values (
lv_id(c_k),
lv_name(c_k),
lv_memo(c_k)
)
;
commit;

exit when lv_id.count < CN_BATCH_SIZE;

end loop;

close cur_tmp;

if cur_tmp%isopen then
close cur_tmp;
end if;

end;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
消耗时长

使用 bulk collect 和 forall 性能均有大幅度提高。
---------------------

oracle 批处理 bulk collect 带来的性能优势的更多相关文章

  1. Oracle forall bulk collect批量数据更新

    对于数据量较大的插入操作可采用此种方法操作,注意: limit减少内存占用,如果数据量较大一次性全部加载到内存中,对PGA来说压力太大,可采用limit的方法一次加载一定数量的数据,建议值通常为100 ...

  2. ORACLE fetch bulk collect into limit

    DECLARE TYPE rr IS REF CURSOR; TYPE r_emp IS RECORD( empno ), ename ), job ), mgr ), hiredate DATE, ...

  3. oracle中bulk collect into用法

    通过bulk collect减少loop处理的开销 采用bulk collect可以将查询结果一次性地加载到collections中. 而不是通过cursor一条一条地处理. 可以在select in ...

  4. PLSQL_性能优化系列11_Oracle Bulk Collect批处理

    2014-10-04 Created By BaoXinjian

  5. PL/SQL批处理语句(BULK COLLECT子句和FORALL语句)

    Oracle为PL/SQL中的SQL相关功能提供了FORALL语句和BULK COLLECT子句,显著的增强了SQL相关功能.这两个语句一起被称作PL/SQL的批处理语句.Oracle为什么要提供这两 ...

  6. bulk collect 在KingbaseES和Oracle的使用方法比较

    概述 BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNI ...

  7. Oracle数据库之FORALL与BULK COLLECT语句

    Oracle数据库之FORALL与BULK COLLECT语句 我们再来看一下PL/SQL块的执行过程:当PL/SQL运行时引擎处理一块代码时,它使用PL/SQL引擎来执行过程化的代码,而将SQL语句 ...

  8. oracle学习之bulk collect用法

    通过bulk collect减少loop处理的开销,使用Bulk Collect提高Oracle查询效率 Oracle8i中首次引入了Bulk Collect特性,该特性可以让我们在PL/SQL中能使 ...

  9. ORACLE PL/SQL开发--bulk collect的用法 .

    刚刚在inthirties老大的博客里看到这篇文章,写的不错,正好自己最近在学习PL/SQL,转过来学习学习. ============================================ ...

随机推荐

  1. truncate与delete删除数据的区别

  2. 四两拨千斤,ARM是如何运作、靠什么赚钱的

    在智能手机.平板大行其道的今天,ARM这个名字我们几乎每天都要见到或者听到几次,作为编辑的我更是如此,每天涉及到的新闻总是或多或少跟ARM扯上关系,它还与Intel.AMD.NVIDA等公司有说不清道 ...

  3. Windows 08 R2_组策略

    目录 目录 组策略 组策略对象GPO 实验一组策略的计算机配置 实验二组策略的用户配置 实验三首选设置 实验四组策略更改计算机桌面 常用的组策略管理模块策略 限制用户运行指定的Windows程序 隐藏 ...

  4. spark为什么比mapreduce运行速度快很多

    比较重要的2个原因 –            1.基于内存 mapreduce任务每次都会把结果数据落地到磁盘,后续有其他的job需要依赖于前面job的输出结果,这里就需要进行大量的磁盘io操作,获取 ...

  5. Django--Aadmin入门

  6. python3反转列表的三种方式

    1.内建函数 reversed() li = [1,2,3,4,5,6] a = list(reversed(li)) print(a) 注意:reversed()函数返回的是一个迭代器,而不是一个L ...

  7. 【html】 两栏对比网页,同时滚动

    有的时候需要左右对比环境,而且希望能同时滚动,如下这么拼接就可以了 <html> <head><meta http-equiv="content-type&qu ...

  8. byte与base64string的相互转化以及加密算法

    //在C#中 //图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = new Memory ...

  9. inno setup静默安装

    [Code] //关键代码静默安装 procedure InitializeWizard(); begin   //不显示边框,这样就能达到不会闪两下了   WizardForm.BorderStyl ...

  10. 服务器oracle数据库定时备份

    首先要先建立一个.bat的文件  然后执行这个bat文件 测试是否能得到这个收据库的打包文件. bat文件内容: @echo off@color bdel /f /s /q D:\oracle\bac ...