简体   繁体   中英

Updating a table with 4 million rows of data

I have been asked to provide an update script to update the value of a column. Currently, the table stores 4 millions of records. The update logic is quite simple, just need to set the value of each record in that column to be an empty string. eg

UPDATE table_name
SET column = '',

On one of our testing environment, it took more than 4 hours to execute this script and it is not even finished.

One thing to note is that, our product environment needs to be online during the script execution time, which means we allow user continue to write into the table at the same time.

I do consider dropping the column and re-adding it back with a default value. But that's gonna break the our prod environment for sure.

Is there any performance improvement I can apply in this case to reduce the execution time down to an hour? eg like updating the record in batches of 5000?

Follows a example, one way what the update could be done by range.

DECLARE @MaxKey as bigint;
DECLARE @RangeStart as bigint;
DECLARE @RangeEnd as bigint;
DECLARE @pass as int
DECLARE @stop as bit

--max key from your table
select  @MaxKey = MAX(PrimaryKey) from dbo.MyTable 

--the range update
SET @pass = 5000;

--variables to set the criteria WHERE
SET @RangeStart = 0
SET @RangeEnd = @pass

--variable to stop the loop
SET @stop = 0

While @stop = 0
Begin

    SELECT @RangeStart, @RangeEnd

    --update the field in range variable @pass
    update dbo.MyTable set myfield = '' where PrimaryKey between @RangeStart and @RangeEnd

    --criteria to stop the loop
    if @MaxKey < @RangeEnd
        SET @stop = 1

    --incrise the range
    SET @RangeStart = @RangeStart + @pass;

    SET @RangeEnd = @RangeEnd + @pass;

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