Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed update in Oracle PL/SQL

I have a simple PL/SQL procedure, with one cursor and I iterate over it. In each iteration I make an UPDATE statement (after performing some business logic to the data).

However, if there are lots of iterations (tens of thousands), this can get pretty slow, because in every iteration there is single UPDATE statement.

Is there a way to somehow "delay" those updates so that they are executed all at once (and thus much faster).

Edit: Oracle 11

like image 531
Ondrej Skalicka Avatar asked Dec 09 '22 03:12

Ondrej Skalicka


1 Answers

If you can't figure out how to use straight SQL (avoiding the for loop all together) then you will likely be able to improve performance using the BULK Collection features of pl/sql.

Example article here.

Syntax excerpt

 LOOP
    FETCH c_orders
    BULK COLLECT INTO v_order_ids, v_currency_codes, v_amounts_local
    LIMIT 100;
    EXIT WHEN v_row_count = c_orders%ROWCOUNT;
    v_row_count := c_orders%ROWCOUNT;
    FOR i IN 1..v_order_ids.count LOOP
      v_amounts_usd(i) := currency_convert (v_amounts_local(i),
                                            v_currency_codes(i));
    END LOOP;
    FORALL i IN 1..v_order_ids.count
      UPDATE open_orders /* bulk bind */
      SET    amount_usd = v_amounts_usd(i)
      WHERE  order_id = v_order_ids(i);
  END LOOP;
like image 72
Brian Avatar answered Jan 02 '23 21:01

Brian