How to perform bulk delete in PL/SQL for better performance?

Links: http://www.dbatools.net/experience/oracle-bulk-delete.html

    In Pro*C or OCI, we can use an host array as a bind value to submit array operations, it will reduce the network round trips, user calls and execute count, it can greatly improve the performance. In Java there is a "executeBatch" interface, and Perl also support bulk operation when using latest Oracle DBD (version 1.19 or above) with "execute_array" interface.

    I will tell you how to perform bulk delete in PL/SQL now, just copy and modify it as your requests.

DECLARE
  TYPE ARRROWID IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
  tbrows ARRROWID;
  row    PLS_INTEGER;
  cursor delete_table is select row_id from testobj_rowid ORDER BY ROWID;
BEGIN
  open delete_table;
  loop
     fetch delete_table bulk collect into tbrows limit 250;
     FORALL row IN 1 .. tbrows.count()
       DELETE TEST.TESTOBJ WHERE rowid = tbrows(row);
     commit;
     -- dbms_lock.sleep(1); /* if delete too fast */
     exit when delete_table%notfound;
  end loop;
  close delete_table;
END;
/

    Now you should be able to write out bulk insert or bulk update code, it's easy.

« Previous | Main | Next »

Powered by
Movable Type 5.01