简体   繁体   English

更新程序名称-数百万条记录

[英]Update Program Name - Multi Million Records

I need to update 5 million rows in the account table. 我需要更新帐户表中的500万行。

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

However, regular update was running and running for 1.5 hours. 但是,常规更新正在运行,并且运行了1.5个小时。 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. 我更喜欢1个脚本。 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. 请为字段progid添加索引。

  2. Add one more condition to filter records where name <> 'NEW PROGRAM NAME' 添加一个条件来过滤名称为<>'NEW PROGRAM NAME'的记录

  3. I do not recommend you update 5 million rows by one script. 我不建议您通过一个脚本更新500万行。 You should update 5000 records per batch to avoid row locks. 您应该每批更新5000条记录,以避免行锁定。

  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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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