Bulk Insert into Oracle database: Which is better: FOR Cursor loop or a simple Select?

前端 未结 8 1388
广开言路
广开言路 2020-12-04 22:32

Which would be a better option for bulk insert into an Oracle database ? A FOR Cursor loop like

DECLARE
   CURSOR C1 IS SELECT * FROM FOO;
BEGIN
   FOR C1_R         


        
8条回答
  •  借酒劲吻你
    2020-12-04 22:53

    As you can see by reading the other answers, there are a lot of options available. If you are just doing < 10k rows you should go with the second option.

    In short, for approx > 10k all the way to say a <100k. It is kind of a gray area. A lot of old geezers will bark at big rollback segments. But honestly hardware and software have made amazing progress to where you may be able to get away with option 2 for a lot of records if you only run the code a few times. Otherwise you should probably commit every 1k-10k or so rows. Here is a snippet that I use. I like it because it is short and I don't have to declare a cursor. Plus it has the benefits of bulk collect and forall.

    begin
        for r in (select rownum rn, t.* from foo t) loop
            insert into bar (A,B,C) values (r.A,r.B,r.C);
            if mod(rn,1000)=0 then
                commit;
            end if;
        end;
        commit;
    end;
    

    I found this link from the oracle site that illustrates the options in more detail.

提交回复
热议问题