批量提交技术的优点
使用批量提交技术会减少PL/SQL引擎与SQL引擎的交互次数。减少CPU的使用。
PL/SQL在调用SQL时需要将值传入到SQL引擎当中。(如下图)批量提交将数组中的值全部提取出来,一次行传给SQL引擎。而不是每次取一条,将该条数据传入到SQL引擎,再读取下一条。
file:///C:/DOCUME%7E1/ADMINI%7E1.WWW/LOCALS%7E1/Temp/msohtmlclip1/01/clip_image001.jpg 注:参考于<<SQL语言艺术>>
实验结果
file:///C:/DOCUME%7E1/ADMINI%7E1.WWW/LOCALS%7E1/Temp/msohtmlclip1/01/clip_image003.gif
具体数据: 写法
| | | | 32.39 | 6.71 | | 1.22 | 0.89 | | 1.2 | 1.8 |
实验方法1 制造一个数组 2 将数组中的数据插入到数据库的表中 3 根据SQL_TRACE收集的数据做比对
三种书写方法与所得数据1 现有写法DECLARE
TYPE POPULATION_TYPE IS TABLE OF NUMBER;
number_collect POPULATION_TYPE;
BEGIN
SELECT LEVEL
BULK COLLECT INTO number_collect
FROM dual
CONNECT BY LEVEL<=200000 ;
FORi IN number_collect.first ..number_collect.last
LOOP
INSERT INTO
test_bulk
VALUES(number_collect(i));
END LOOP ;
commit ;
END;
统计数据
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call
count
cpu
elapsed
disk
query
current
rows ------- ------
-------- ---------- ---------- --------------------
---------- Parse
29
0.00
0.00
0
0
0
0 Execute 200028
6.22
31.87
0
356
205620
200004 Fetch
40
0.49
0.51
0
62
0
200018 ------- ------
-------- ---------- ---------- --------------------
---------- total
200097
6.71
32.39
0
418
205620
400022
2 Oracle推荐的写法DECLARE
TYPE POPULATION_TYPE IS TABLE OF NUMBER;
number_collect POPULATION_TYPE;
BEGIN
SELECT LEVEL
BULK COLLECT INTO number_collect
FROM dual
CONNECT BY LEVEL<=200000 ;
FORALL i IN number_collect.first ..number_collect.last
INSERT INTO
test_bulk
VALUES(number_collect(i));
commit ;
END;
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call
count
cpu
elapsed
disk
query
current
rows ------- ------
-------- ---------- ---------- --------------------
---------- Parse
12
0.01
0.00
0
0
0
0 Execute
12
0.38
0.73
0
627
3666
200006 Fetch
7
0.50
0.49
0
9
0
200003 ------- ------
-------- ---------- ---------- --------------------
---------- total
31
0.89
1.22
0
636
3666
400009 3 替代写法
DECLARE
TYPE POPULATION_TYPE IS TABLE OF NUMBER;
number_collect POPULATION_TYPE;
v_start NUMBER;
v_end
NUMBER ;
BEGIN
SELECT LEVEL
BULK COLLECT INTO number_collect
FROM dual
CONNECT BY LEVEL<=200000 ;
v_start :=number_collect.first ;
v_end
:=number_collect.last;
INSERT INTO test_bulk
SELECT number_collect(v_start+LEVEL)
FROM dual
CONNECT BY LEVEL<=v_end+1 - v_start;
COMMIT ;
END;
统计数据: OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call
count
cpu
elapsed
disk
query
current
rows ------- ------
-------- ---------- ---------- --------------------
---------- Parse
15
0.00
0.00
0
0
0
0 Execute
15
0.66
1.27
0
606
3439
200004 Fetch
13
0.54
0.53
0
20
0
200004 ------- ------
-------- ---------- ---------- --------------------
---------- total
43
1.20
1.80
0
626
3439
400008
实验结果
|