优化慢速更新查询

时间:2015-08-14 18:31:14

标签: sql sql-server sql-server-2008 sql-update

我有两张桌子:

1) Testone - 有100k行,1个索引和6个非聚集索引

2) Testtwo - 拥有316百万行,1个索引和4个非聚集索引

由于在执行此查询时花费了更多时间,我有更新语句需要优化以下内容,请您帮我解决最佳方法..

update mp 
set mp.ModelInfoID = mi.ModelInfoID 
from Testone mp 
inner join Testtwo mi on mp.ModelNumberStr = replace(replace(mi.Model, '/', ''), '-', '') 
where MemberID is not null and mp.ModelInfoID is null

1 个答案:

答案 0 :(得分:0)

从优化这一开始 看看查询计划
MemberID,mp.ModelInfoID和mi.ModelInfoID上的索引将有帮助

select mp.ModelInfoID, mi.ModelInfoID 
  from Testone mp 
  join Testtwo mi 
    on mp.ModelNumberStr = replace(replace(mi.Model, '/', ''), '-', '') 
   and MemberID is not null 
   and mp.ModelInfoID is null
   and mi.ModelInfoID is not null -- no need to update if they are the same

Cleary the replace(替换(mi.Model,'/',''),' - ','')将成为瓶颈。带索引的计算列可以起到作用。