Documente Academic
Documente Profesional
Documente Cultură
Improve the performance of multi-row SQL operations by an order of magnitude or more with bulk/array processing in PL/SQL!
CREATE OR REPLACE PROCEDURE upd_for_dept ( dept_in IN employee.department_id%TYPE ,newsal_in IN employee.salary%TYPE) IS CURSOR emp_cur IS SELECT employee_id,salary,hire_date FROM employee WHERE department_id = dept_in; BEGIN FOR rec IN emp_cur LOOP
SQL Engine
SQL Engine
BULK COLLECT
Use with implicit and explicit queries. Move data from tables into collections.
In both cases, the "back back" end processing in the SQL engine is unchanged.
Same transaction and rollback segment management Same number of individual SQL statements will be executed. But BEFORE and AFTER statement-level triggers only fire once per FORALL statement.
Copyright 2000-2007 Steven Feuerstein - Page 4
bulkcoll.sql bulktiming.sql
WARNING! BULK COLLECT will not raise NO_DATA_FOUND if no rows are found. Always check contents of collection to confirm that something was retrieved.
Definitely the preferred approach in production applications with large or varying datasets.
Oracle9i
Allows you to continue past errors and obtain error information for each individual operation (for dynamic and static SQL).
CREATE OR REPLACE PROCEDURE load_books (books_in IN book_obj_list_t) IS bulk_errors EXCEPTION; PRAGMA EXCEPTION_INIT ( bulk_errors, -24381 ); BEGIN FORALL indx IN books_in.FIRST..books_in.LAST Allows processing of all SAVE EXCEPTIONS rows, even after an INSERT INTO book values (books_in(indx)); error occurs. EXCEPTION WHEN BULK_ERRORS THEN FOR indx in 1..SQL%BULK_EXCEPTIONS.COUNT New cursor LOOP attribute, a pseudolog_error (SQL%BULK_EXCEPTIONS(indx)); END LOOP; collection END;
bulkexc.sql
DBMS_ERRLOG (Oracle10gR2)
Allows DML statements to execute against all rows, even if an error occurs.
The LOG ERRORS clause specifies how logging should occur. Use the DBMS_ERRLOG package to associate a log table with DML operations on a base table.
Much faster than trapping errors, logging, and then continuing/recovering. You should consider using LOG ERRORS with FORALL (instead of SAVE EXCEPTIONS) so that you can obtain all error information!
But there are some differences in behavior.
Copyright 2000-2007 Steven Feuerstein - Page 9
You trade off increased complexity of code for dramatically faster execution.
But in Oracle Database 10g and above, the compiler will automatically optimize cursor FOR loops to BULK COLLECT efficiency. No need to convert unless the loop contains DML.