我有一个包含 900 万条记录的表,我需要遍历每一行,并且需要在每次迭代中插入多个表。
我的示例查询是
//this is the table with 9 million records
create table tablename
(
ROWID INT IDENTITY(1, 1) primary key ,
LeadID int,
Title varchar(20),
FirstName varchar(50),
MiddleName varchar(20),
Surname varchar(50)
)
declare @counter int
declare @leadid int
Declare @totalcounter int
set @counter = 1
Select @totalcounter = count(id) from tablename
while(@counter < @totalcounter)
begin
select @leadid = leadid from tablename
where ROWID = @counter
--perform some insert into multiple tables
--in each iteration i need to do this as well
select * from [sometable]
inner join tablename where leadid = @leadid
set @counter = @counter + 1
end
这里的问题是这花费了太长时间,尤其是每次迭代的连接。
有人可以帮我优化这个。