I have two tables. They both have identical structures except table2 has an additional column. I currently copy data from table1 into table2 using a stored proc, as shown below.
However, due to the sheer number of records (20million+), and the structure of the stored proc, this currently takes a couple of hours to run.
Does anyone have any suggestions on how to optimize the code?
CREATE PROCEDURE dbo.insert_period @period INT AS
DECLARE @batchsize INT
DECLARE @start INT
DECLARE @numberofrows INT
SELECT @numberofrows = COUNT(*) from daily_table
SET @batchsize = 150000
SET @start = 1
WHILE @start < @numberofrows
BEGIN
INSERT INTO dbo.main_table WITH (TABLOCK) (
col1,
col2,
....,
col26,
time_period
)
SELECT *, @period FROM dbo.daily_table
ORDER BY id
OFFSET @start ROWS
FETCH NEXT @batchsize ROWS ONLY
SET @start += @batchsize + 1
END
The id that I am using here is not unique. The table itself does not have any keys or unique id's.