简体   繁体   中英

Update Program Name - Multi Million Records

I need to update 5 million rows in the account table.

update account set name = 'NEW PROGRAM NAME' where progid = 1111;

However, regular update was running and running for 1.5 hours. I had to cancel it and then it took time to cancel. It was a mess.

I need the expert advice on how to update quickly and in as less time as possible.

I prefer 1 script. But I was thinking may be:

update account set name = 'NEW PROGRAM NAME' where progid = 1111 and rownum <= 500000;

And then commit ?

You are very close:

update account
    set name = 'NEW PROGRAM NAME'
    where progid = 1111 and name <> 'NEW PROGRAM NAME' and
          rownum <= 5000;

You can repeatedly do this update, with commits in between.

  1. Please add the index for the field progid.

  2. Add one more condition to filter records where name <> 'NEW PROGRAM NAME'

  3. I do not recommend you update 5 million rows by one script. You should update 5000 records per batch to avoid row locks.

  4. If the database design can be change, you should perform database normalization. Create a table 'program' to store the progid and name, the table 'account' only store the field progid, so that you just update one of the record in table 'program'

     While (Select Count(1) From account Where progid = 1111 and name <> 'NEW PROGRAM NAME') > 0 Begin update account set name = 'NEW PROGRAM NAME' where progid = 1111 and name <> 'NEW PROGRAM NAME' and rownum <= 5000; End 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM