一括バインディング(FOALL)

3183 ワード

1.一括バインディングとは、単一のSQL操作を実行することで、すべてのセット要素のデータを転送し、データ処理速度を大幅に速くし、プログラムの性能を向上させることができます。Bulk Collectサブ句とForALLサブ句を使用して完成されます。
2.ForAll:この子文は大量のDLL操作を実行するだけに適用され、1つの文を修飾することができます。3つの文法を含みます。
a.
ForAll index in lower_bound..upper_bound
sql_statement;
  :
create table my_demo(
 mid number(6) primary key,
 mname varchar2(50)
);

declare
 type my_index_table1 is table of my_demo.mid%type index by binary_integer;
 type my_index_table2 is table of my_demo.mname%type index by binary_integer;
 my_id my_index_table1;
 my_name my_index_table2;
 start_time number(10);
 end_time number(10);
begin
 for i in 1..1000 loop
  my_id(i):=i;
  my_name(i):=to_char(i)||'NAME';
 end loop;
 start_time:=dbms_utility.get_time;
 forall i in 1..1000 
  insert into My_DEMO values(my_id(i),my_name(i));
 end_time:=dbms_utility.get_time;
 
 dbms_output.put_line(end_time-start_time);
end;

b.
ForAll index in indices of collection
[Between lower_bound.and. upper.bound]
sql_statement;(      ,    )
  :
declare
  type id_table_type is table of demo%rowtype index by binary_integer;
  id_table id_table_type;
  type index_table_type is table of Pls_Integer index by binary_integer;
  index_table index_table_type;
begin
  select object_id,object_name bulk collect into id_table from all_objects where rownum<=10;
  index_table(1):=1;
  index_table(2):=2;
  index_table(4):=6;
  index_table(6):=8;
  index_table(8):=10;
  forall i in indices of index_table--  index_table  1,2,4,6,8
         insert into demo(did) values(id_table(i).did);--id_table(1,2,4,6,8)
         
  
end; 

c.
ForAll index in values of index_collection
sql_statement;(        )
  :
declare
  type id_table_type is table of demo%rowtype index by binary_integer;
  id_table id_table_type;
  type index_table_type is table of PLS_Integer index by binary_integer;
  index_table index_table_type;
begin
  select object_id,object_name bulk collect into id_table from all_objects where rownum<=10;
  index_table(1):=1;
  index_table(2):=2;
  index_table(4):=6;
  index_table(6):=8;
  index_table(8):=10;
  forall i in values of index_table--  index_table    1,2,6,8,10
         insert into demo(did) values(id_table(i).did);--id_table(1,2,6,8,10)
         
  
end; 
3.Bulk Collect:このサブ句は大量データを取得するために用いられ、Select、Fetch、DMLはサブカルに返すためにのみ使用されます。
declare
       type eaa is record(
            empno scott.emp.empno%type,
            ename scott.emp.ename%type,
            sal scott.emp.sal%type
       );
       type emp_table_type is table of scott.emp%rowtype
       index by binary_integer;
       emp_table emp_table_type;
       
       type aa is table of scott.emp%rowtype;
       a aa;
       
       type bb is varray(1000) of scott.emp%rowtype;
       b bb;
       
begin
       select * bulk collect into emp_table from scott.emp;
       for i in 1..emp_table.count loop
        dbms_output.put_line(emp_table(i).ename);
       end loop;
       
   
       
       dbms_output.new_line;
             select * bulk collect into b from scott.emp;
       for i in 1..b.count loop
        dbms_output.put_line(b(i).ename);
       end loop;
end;