0

各行をループし、反復ごとに複数のテーブルに挿入する必要がある 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

ここでの問題は、特に各反復での結合に時間がかかりすぎることです。

誰かがこれを最適化するのを手伝ってくれませんか。

4

2 に答える 2

0

の行sometableと同じ行をすべて検索しようとしているようです。その場合、単純な結合が機能するはずですleadidtablename

select t2.*
from tablename t2 inner join sometable t2
     on t1.leadid=t2.leadid

リードのインデックスがある限り、問題はないはずです

あなたは本当に何をしようとしていますか?

于 2013-11-15T11:24:59.970 に答える