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. python3安装pdfminer并使用

    1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...

  2. hashmap:cr:csdn

    HashMap相关问题 1.你用过HashMap吗?什么是HashMap?你为什么用到它? 用过,HashMap是基于哈希表的Map接口的非同步实现,它允许null键和null值,且HashMap依托 ...

  3. MySQL &lt; &gt; 等用法

  4. MAC使用命令行打包出ipa包-通过xcodeproj

    参考 : https://www.jianshu.com/p/32af2f71b4e5--老了,里面的一些命令现在都没有了,但可以借鉴思路 https://www.jianshu.com/p/004c ...

  5. xcodebuild 自动化打包

    altool 文档 使用xcode自带的xcodebuild 命令通过脚本进行打包 打包->导出ipa, 两行关键的脚本代码 1.Archive xcodebuild archive -arch ...

  6. Codeforces #576 Rectangle Painting 1 | div1D | div2F | DP | Rustlang

    原题链接 大意 n*n正方形 有黑有白 每次可以选择一个 矩形把它全变成白色,代价是max(长,宽) 求吧 整个正方形 全变白 的最小代价 数据范围 n <= 50 题解 首先如果 我们刷了两个 ...

  7. [Java] 缓存池

    new Integer(123) 与 Integer.valueOf(123) 的区别在于: new Integer(123) 每次都会新建一个对象: Integer.valueOf(123) 会使用 ...

  8. TP框架实现文件的下载(主要解决文件存在中文文件名的问题)

    namespace Home\Controller; use Think\Controller; use Org\Net\Http; class IndexController extends Con ...

  9. HDU 3571 N-dimensional Sphere( 高斯消元+ 同余 )

    N-dimensional Sphere Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. Hibernate4教程二:基本配置

    可编程的配置方式一: 如果在配置cfg.xml的时候,不想在里面配置hbm.xml怎么办呢?可在程序里使用可编程的配置方式,也就是使用程序来指定在cfg.xml里面的配置信息,不推荐这种方式.如下: ...