+ -
当前位置:首页 → 问答吧 → bulk collect into批量取出和 插入,这个语句写的哪里错误?

bulk collect into批量取出和 插入,这个语句写的哪里错误?

时间:2011-09-15

来源:互联网

为了测试 bulk collect into, 做了如下动作:


create table aa as select * from dba_objects where 1 > 2;
insert into aa select * from dba_objects;
insert into aa select * from aa;
select count(*) from aa;                 到了160万个记录


create table aa2 as select * from dba_objects where 1 > 2;




2种方法,插入记录,记录时间,
方法1:
insert into aa2 select * from aa;         -- 花费 4秒,Executed in 4.259 seconds

方法2:
declare
   type aa_table_type is table of risk.aa%rowtype  index by pls_integer;
   aa_table aa_table_type;
begin
   select * bulk collect into aa_table from aa;   
   for i in aa_table.first..aa_table.last loop  
      insert into aa2 values( aa_table(i).OWNER, aa_table(i).OBJECT_NAME, aa_table(i).SUBOBJECT_NAME,
                                 aa_table(i).OBJECT_ID, aa_table(i).DATA_OBJECT_ID, aa_table(i).OBJECT_TYPE,
                                 aa_table(i).CREATED, aa_table(i).LAST_DDL_TIME, aa_table(i).TIMESTAMP,
                                 aa_table(i).STATUS, aa_table(i).TEMPORARY, aa_table(i).GENERATED,
                                 aa_table(i).SECONDARY   );
   end loop;
end;

--  花费了 55秒

为什么效率这么差?

作者: 茫茫蒙古草原   发布时间: 2011-09-15



LZ,你目前使用的PL/SQL代码有没有使用批量插入当然慢了, 这种问题也是老问题了, 你可以用一下的方法测试一下!

declare

  type aa_table_type is table of aa%rowtype index by pls_integer;
  aa_table aa_table_type;
  
  cursor cur is select * from aa;
  
begin

  open cur;

  loop
     fetch cur bulk collect into aa_table limit 1000;
     
     forall i in aa_table.first..aa_table.last
            insert into aa2 values aa_table(i);
         
     exit when cur%notfound;
     
  end loop;
  
  close cur;
  
end;


作者: bell6248   发布时间: 2011-09-15

使用版主的方式,用时20秒 ,我改写成如下:

declare
  type aa_wen_table_type is table of aa_wen%rowtype index by pls_integer;
  aa_wen_table aa_wen_table_type;
begin
     select * bulk collect into aa_wen_table from aa_wen;   
     forall i in aa_wen_table.first..aa_wen_table.last   
       insert into aa_wen2 values aa_wen_table(i);
end;

也是20秒,为什么 没有直接的 插入 insert into aa_wen2 select * from aa_wen; 快呢?

和索引有关吗?我 的表里没有索引

作者: 茫茫蒙古草原   发布时间: 2011-09-15