我想一次更新最多20000条记录到一个表里,用update执行速度很慢,想请教一下,采用什么方式能较快的批量更新。

解决方案 »

  1.   

    写个plsql,每2000条commit一次,应该很快的吧
      

  2.   

    bulk collect...
    forall ....
      

  3.   


    这个没用过,我的数据是从SQL Server数据库查询出来的,更新到Oracle数据库,所以是先取到数据,然后再去更新。请教一下,使用您提供的方式进行Update,该如何操作。
      

  4.   


    --给你个模版 写个plsql  
    alter tabke tablename nologging;  --更新不写入redolog declare
    v_count number;
    cursor c1 select ......;
    begin
    v_count:=0;
    for rec in c1 loop
    update .....;
    if (v_count>=1000) then
    commit;
    v_count:=0;
    end if;
    end;
      

  5.   

    2楼说的很对,批量添加确实速度很快,我测试了一下,插入50万条仅仅用了0.03秒左右,但是批量更新我一直很头疼,批量(不知道是不是)更新了100万条记录竟然用了200多秒。
    不过我想,你只有2万条数据,如果对性能不是很严格要求的话,下面的方法应该能满足你的需求,毕竟才2万条记录。
    1. 直接使用sql的for循环
       for i in (select t,tt from temp_test) loop  
          update test2 set test2.tt = i.tt where test2.t = i.t;
        end loop;
       commit;
    2.定义一个集合
       for i in 1 .. t.count loop
          update test2 set test2.tt = t(i).tt where t(i).t = test2.t;
       end loop;
       commit;
    备注:只是个人意见,希望对你有用,也欢迎大家对我指正,提出更佳方案。