1)记住批量绑定是什么 2)用什么完成的,各个工具之间都有什么方法及使用即可
批量绑定:执行单词SQL操作能传递所有集合元素的数据,使用forall,bulk_collect语句完成,
bulk_collect子句用于取得批量数据,只能用于select,fetch和dml返回字句中,
forall语句只试用于执行批量的dml执行,
好处:可以极大地提高数据处理速度,提高应用程序的性能,
demo示例:
create table demo (
id number(6) primary key,name varchar2(10)
);
不使用批量绑定
declare
type id_table_type is table of number(6) index by binary_integer;
type name_table_type is table of varchar2(20) index by binary_integer;
id_table id_table_type;
name_table name_table_type;
start_time number(10);
end_time number(10);
begin
for i in 1..5000 loop
id_table(i):=i;
name_table(i):='Name'||to_char(i);
end loop;
start_time:=dbms_utility.get_time();
for i in 1..id_table.count loop
insert into demo values(id_table(i),name_table(i));
end loop;
end_time:=dbms_utility.get_time();
dbms_output.put_line('Alltime(s):'||to_char((end_time-start_time)/100));
end;
SQL> /
Alltime(s):.24
PL/SQL procedure successfully completed
使用批量绑定
declare
type id_table_type is table of number(6) index by binary_integer;
type name_table_type is table of varchar2(20) index by binary_integer;
id_table id_table_type;
name_table name_table_type;
start_time number(10);
end_time number(10);
begin
for i in 1..5000 loop
id_table(i):=i;
name_table(i):='Name'||to_char(i);
end loop;
start_time:=dbms_utility.get_time();
forall i in 1..id_table.count
insert into demo values(id_table(i),name_table(i));
end_time:=dbms_utility.get_time();
dbms_output.put_line('Alltime(s):'||to_char((end_time-start_time)/100));
end;
SQL> /
Alltime(s):.09
PL/SQL procedure successfully completed
forall有三种语法:
1)forall index in lower_bound..upper_bound
sql_statement;
2)forall index in indices of collection 用于处理null值
[between lower_bound.and. upper_bound]
sql_statement;
3)forall index in values of index_collection
sql_statement;
该语句还有一个方法是取部分的下标,sql%bulk_rowcount属性用于取得在执行批量绑定操作时第i个元素所作用的行数,
bulk_collect子句用于取得批量数据,用于select into ,fetch into,dml子句。将批量数据放于pl/sql集合变量中。
1)select into中使用bulk_collect子句,select into只能返回一行数据,但是用bulk_collect却可以一次将select语句的多行结果检索到集合变量中,
declare
type emp_table_type is table of emp%ROWTYPE index by binary_integer;
emp_table emp_table_type;
begin
select * bulk_collect into emp_table from emp where deptno=&no;
for i in 1..emp_table.count loop
dbms_output.put_line('Employee name :'||emp_table(i).ename);
end loop;
end;
/
2)dml返回字句中使用
declare
type emp_table_type is table of emp%ROWtype index by binary_integer;
emp_table emp_table_type;
begin
delete from emp where deptno=&no
returning ename bulk collect into ename_table;
for i in 1..emp_table.count loop
dbms_output.put_line(emp_table(i).ename);
end loop;
end;
/
本文转自 aklaus 51CTO博客,原文链接:http://blog.51cto.com/aklaus/1966342