In SQL Server, I have a table (dbo.MYTABLE) containing 12 million rows and 5.6 GB storage.
I need to update a varchar(150) field for each record.
I perform the UPDATE operation in WHILE loop, updating 50K rows in each iteration.
Transaction log seems not to free after each iteration and keeps growing. Even, after all the UPDATE process finished, Transaction Log space is not returned.
My question is that, why used transaction log space never decreases even UPDATE is complete. Code is below:
DECLARE @MAX_COUNT INT;
DECLARE @COUNT INT;
DECLARE @INC INT;
SET @COUNT = 0;
SET @INC = 50000;
SELECT @MAX_COUNT=MAX(ID) FROM dbo.MYTABLE(NOLOCK)
WHILE @COUNT <= @MAX_COUNT
BEGIN
UPDATE dbo.MYTABLE
SET NEW_NAME = REPLACE(NAME,' X','Y'))
WHERE ID > @COUNT AND ID<=( @COUNT + @INC)
SET @COUNT = (@COUNT + @INC)
END