Basically I need to run this on a table with 40 million rows, updating every row at once will crash, so I want to batch the query so that if it crash, it can re-run the query and it would skip the finished batch and just continue with the ones left over.
UPDATE [table]
SET [New_ID] = [Old_ID]
What is the fastest way to do this? Here is how the table is created:
CREATE TABLE [table](
[INSTANCE_ID] [int] NOT NULL,
[table_ID] [bigint] IDENTITY(1,1) NOT NULL,
[old_ID] [bigint] NOT NULL,
[new_ID] [bigint] NOT NULL,
[owner_ID] [int] NOT NULL,
[created_time] [datetime] NULL
) ON [PRIMARY]
There are also indexes on created_time, owner_ID.
EDIT: My update statement is EXACTLY as shown, I literally just need to copy every entry in old_id into new_id for 40 million rows.
5条答案
按热度按时间inkz8wg91#
yftpprvb2#
M.Ali's suggestion will work, but you will end up with degrading performance as you work through the 40M records. I would suggest a better filter to find the records to update in each pass. This would assume you have a primary key (or other index) on your identity column:
This approach will allow each iteration to be as fast as the first. And if you don't have a valid index you need to fix that first.
slmsl1lt3#
100000 may work better for the top.
Since NewID and OldID is not null then the is null check is not necessary.
hrysbysz4#
Fastest way is to :
Create a temp table and insert all the values from old to temp table using the create(select having condition) statement.
Copy the constraints and refresh the indexes.
Drop the old table.
Rename temp table to original name.
Complete discussion is available on this link
crcmnpdw5#
I have implemented a logic that allows me to update millions of records in a short amount of time for a single column. I would like to share this logic and ask for feedback on its effectiveness. Here's an explanation of the logic:
Step 1:
step 2:
Step 3:
Step 4:
Step 5:
I would appreciate any feedback or suggestions for improvement on this logic. Is there a better approach or any potential issues with this implementation? Thank you in advance.