【问题标题】:MS sql server looping through huge tableMS sql server 循环遍历巨大的表
【发布时间】:2013-11-15 19:24:59
【问题描述】:

我有一个包含 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

这里的问题是耗时太长,尤其是每次迭代的连接。

谁能帮我优化一下。

【问题讨论】:

  • 请说明您想要达到的目标。为什么简单的加入还不够?为什么是循环和计数器?
  • 理想情况下,您以基于集合的方式表达操作并完全避免循环。但目前我们无能为力,因为您向我们展示的只是您的循环基础架构代码。
  • 这个带有INNER JOIN的查询是为了什么??????仅在最后一次迭代中才有意义 - 如果您的存储过程将其作为结果集返回

标签: sql sql-server optimization


【解决方案1】:

是的,您的连接需要很长时间,因为您的两个表之间没有指定连接条件,因此您正在创建笛卡尔积。这肯定需要一段时间。

如果你想优化这个,指定你想加入这些表的地方。

如果仍然很慢,请查看适当的索引。

【讨论】:

    【解决方案2】:

    您似乎正在尝试查找 sometable 中与 tablename 中的行具有相同 leadid 的所有行?如果是这样,一个简单的连接应该可以工作

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

    只要你有一个关于 leaid 的索引,你就不应该有任何问题

    你真正想做什么?

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多