简体   繁体   中英

Update 13 million rows - SQL Server 2008

How can I update 13 million rows in stages by using a cursor or something?

Updating with the current script runs for days and still haven't finished.

There is a row_id field. 1 - 13m

Only one field needs to be updated.

    UPDATE
        [CIPC].[dbo].[tbldirector]
    SET
        [CIPC].[dbo].[tbldirector].ENT_NUM = REG.Ent_Number
    FROM
        [CIPC].[dbo].[tbldirector] DIR
    INNER JOIN
        [Cipc].[dbo].[tblregister]  REG
    ON 
        DIR.ENT_LONGNAME = REG.ENT_NAME

in this case you don't need cursor . You can do it with a loop like this.

    DECLARE @indx int, @StepSize INT

SET @indx = 1
SET @StepSize = 100000

BEGIN TRAN 
    WHILE (EXISTS(SELECT 0 FROM [CIPC].[dbo].[tbldirector] WHERE row_id >= @indx))
    BEGIN
        PRINT 'Going to update indx ' + REPLICATE(CONVERT(VARCHAR, @indx) + ' -- ' + CONVERT(VARCHAR, @indx + @StepSize) + ' | ', 200)
        UPDATE [CIPC].[dbo].[tbldirector]
            SET [CIPC].[dbo].[tbldirector].ENT_NUM = REG.Ent_Number
            FROM [CIPC].[dbo].[tbldirector] DIR
            INNER JOIN [Cipc].[dbo].[tblregister]  REG
                ON DIR.ENT_LONGNAME = REG.ENT_NAME
            WHERE row_id BETWEEN @indx AND @indx + @StepSize
        SELECT @indx = @indx + @StepSize
        SELECT REPLICATE(LEFT(CONVERT(VARCHAR, @indx) + ' | ', 10), 200)
    END
COMMIT

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