How to optimize an update SQL that runs on a Oracle table with 700M rows

后端 未结 5 572
后悔当初
后悔当初 2021-02-06 02:01
UPDATE [TABLE] SET [FIELD]=0 WHERE [FIELD] IS NULL

[TABLE] is an Oracle database table with more than 700 million rows. I cancelled the SQL execution a

5条回答
  •  北海茫月
    2021-02-06 02:15

    Are other users are updating the same rows in the table at the same time ?

    If so, you could be hitting lots of concurrency issues (waiting for locks) and it may be worth breaking it into smaller transactions.

    DECLARE
      v_cnt number := 1;
    BEGIN
     WHILE v_cnt > 0 LOOP
       UPDATE [TABLE] SET [FIELD]=0 WHERE [FIELD] IS NULL AND ROWNUM < 50000;
       v_cnt := SQL%ROWCOUNT;
       COMMIT;
     END LOOP;
    END;
    /
    

    The smaller the ROWNUM limit the less concurrency/locking issues you'll hit, but the more time you'll spend in table scanning.

提交回复
热议问题