June 29, 2007 at 11:18 am
Hi,
I try to update table to asign random number to each row. The table has 4 million rows and each row updated 0.5 second. It will take 555 hours or 23 days to update the table.
Any idea how to speedup the update statement?
Declare @id varchar(64), @SmpID varchar(64)
DECLARE cust_cursor CURSOR FOR SELECT id,SmpID FROM Table_1
OPEN cust_cursor
FETCH NEXT FROM cust_cursor
INTO @id, @SmpID
WHILE @@FETCH_STATUS = 0
BEGIN UPDATE Table_1
SET RND = RAND() * 100000000000000
WHERE id = @id AND SmpID = @SmpID
FETCH NEXT FROM cust_cursor INTO @id, @SmpID
END
CLOSE cust_cursor
DEALLOCATE cust_cursor
Regard,
M.Z.
June 29, 2007 at 2:12 pm
Replace
WHERE id = @id AND SmpID = @SmpID
with:
WHERE
CURRENT OF cust_cursor
In my tests it reduced execution time about 7 times.
June 29, 2007 at 2:51 pm
thanks. It helps.
June 29, 2007 at 5:11 pm
Why even use a cursor for this, you can reinitialize the the rand() for each row and do a single update which would most likely take only a few seconds (assuming you can lock the whole table to do this update, if not you can always do the updates in batch).
July 2, 2007 at 12:01 am
Like Remi says... why use a cursor???
UPDATE TABLE_1
SET Rnd = RAND(CAST(NEWID() AS VARBINARY)) * 10000000000000
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2007 at 7:03 am
But this generates the same random number 4 million times. Maybe that's why he used the cursor.
July 2, 2007 at 7:08 am
Did you actually run the statement before saying that?? The data will be unique for each row.
July 2, 2007 at 7:16 am
Yep. And it doesn't work. The selects below always generate just one record with a count of 4000000.
create table temp1(randnum bigint)
insert into temp1
select top 4000000 rand()*10000000000000
from somebigtable
select randnum,count(*)
from temp1
group by randnum
having count(*)>1
update temp1
set randnum=rand()*10000000000000
select randnum,count(*)
from temp1
group by randnum
having count(*)>1
July 2, 2007 at 7:23 am
To get different values you need to mess with the function newid().
July 2, 2007 at 7:28 am
You need to have another look there fella... I DID use NEWID() in my code... you didn't in your test.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2007 at 7:31 am
Man are you right. I stopped at RAND and didn't bother to look what it's being passed. Sorry.
July 2, 2007 at 7:31 am
Why do you think he used THIS CODE???
RND(CAST(NEWID() AS VARBINARY)) * 10000000000000
July 2, 2007 at 7:36 am
And this is the code that works in 2000 :
SELECT RAND(CAST(NEWID() AS VARBINARY)) * 10000000000000 AS Test FROM master.dbo.SysColumns
July 2, 2007 at 7:40 am
Indeed! Although the SS2K BOL documents allowable seeds for RAND() as limited to integer types. But if varbinary works, so be it.
July 2, 2007 at 7:41 am
Heh... no worries... I've done the same thing when I got in a hurry I even mispelled "RAND" in my first post and had to go back and correct it
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply