views:

53

answers:

1

Hello,

I am trying to copied records from one table to another as fast as possible.

Currently I have a simple cursor loop similiar to this:

FOR rec IN source_cursor LOOP
   INSERT INTO destination (a, b) VALUES (rec.a, rec.b)
END LOOP;

I want to speed it up to be super fast so am trying some BULK operations (a BULK FETCH, then a FORALL insert):

Here is what I have for the bulk select / forall insert.

DECLARE 
  TYPE t__event_rows IS TABLE OF _event%ROWTYPE;
  v__event_rows t__event_rows;

  CURSOR c__events IS
    SELECT * FROM _EVENT ORDER BY MESSAGE_ID;
BEGIN
  OPEN c__events;
  LOOP
    FETCH c__events BULK COLLECT INTO v__event_rows LIMIT 10000;  -- limit to 10k to avoid out of memory

    EXIT WHEN c__events%NOTFOUND;

    FORALL i IN 1..v__event_rows.COUNT SAVE EXCEPTIONS
      INSERT INTO destinatoin
        ( col1, col2, a_sequence) 
        VALUES 
        (  v__event_rows(i).col1,  v__event_rows(i).col2, SOMESEQEUENCE.NEXTVAL );


  END LOOP;
  CLOSE c__events;


END;

My problem is that I'm not seeing any big gains in performance so far. From what I read it should be 10x-100x faster.

Am I missing a bottleneck here somewhere?

+7  A: 

The only benefit your code has over a simple INSERT+SELECT is that you save exceptions, plus (as Justin points out) you have a pointless ORDER BY which is making it do a whole lot of meaningless work. You then don't have any code to do anything with the exceptions that were saved, anyway.

I'd just implement it as a INSERT+SELECT.

Jeffrey Kemp
And you can use a LOG ERRORS clause on the INSERT to save exceptions.
Dave Costa