Improve Bulk Update with Array Processing
DECLARE
CURSOR c_dim_cur IS
SELECT /*+ parallel(10) */
k.col1, t.ID, t.col4
FROM r1.tab1 t, r1.tab2 k
WHERE t.no = k.no
AND t.insert_sysdate < TRUNC (SYSDATE)
AND t.col2 = '123456789'
AND t.no IS NOT NULL;
TYPE c_dim_array_t_1 IS RECORD
(
col1 r1.tab1.col2%TYPE,
id r1.tab1.ID%TYPE,
col4 r1.tab1.col4%TYPE
);
TYPE c_dim_array_t IS TABLE OF c_dim_array_t_1; -- define collection for rows selected
k_dim_rows_max CONSTANT INTEGER := 5000; -- defines the maximum rows per fetch
l_dim_collect c_dim_array_t; -- define variable of rows collection
BEGIN
OPEN c_dim_cur;
LOOP
FETCH c_dim_cur -- fetch up to LIMIT rows from cursor
BULK COLLECT INTO l_dim_collect LIMIT k_dim_rows_max;
FORALL i IN 1 .. l_dim_collect.COUNT -- run update for ALL rows in the collection
UPDATE /*+ index(tab1 PK_tab1IX) parallel(10) */
r1.tab1 f
SET f.col2 = l_dim_collect (i).col1
WHERE f.id = l_dim_collect (i).id
AND f.col4 = l_dim_collect (i).col4;
EXIT WHEN c_dim_cur%NOTFOUND; -- no more rows so exit
COMMIT;
END LOOP;
CLOSE c_dim_cur;
COMMIT;
END;