原文:来自CSDNKamus' Oracle World的文章在PL/SQL中使用Bulk Binds对性能的提高
测试BULK COLLECT的脚本。
代码:--------------------------------------------------------------------------------
SQL> SET TIMING ON;
SQL>
SQL> DECLARE
2 TYPE id_type IS TABLE OF test1.id%TYPE;
3 TYPE description_type IS TABLE OF test1.description%TYPE;
4
5 t_id id_type := id_type();
6 t_description description_type := description_type();
7
8 CURSOR c_data IS
9 SELECT *
10 FROM test1;
11 BEGIN
12 FOR cur_rec IN c_data LOOP
13 t_id.extend;
14 t_description.extend;
15
16 t_id(t_id.last) := cur_rec.id;
17 t_description(t_description.last) := cur_rec.description;
18 END LOOP;
19 END;
20 /
PL/SQL procedure successfully completed
Executed in 2.974 seconds
SQL>
SQL> DECLARE
2 TYPE id_type IS TABLE OF test1.id%TYPE;
3 TYPE description_type IS TABLE OF test1.description%TYPE;
4
5 t_id id_type;
6 t_description description_type;
7 BEGIN
8 SELECT id, description
9 BULK COLLECT INTO t_id, t_description FROM test1;
10 END;
11 /
PL/SQL procedure successfully completed
Executed in 0.371 seconds
SQL>--------------------------------------------------------------------------------
结论:当我们需要将大量的检索结果放入一个collection的时候,使用bulking将比直接使用cursor循环有效的多。