2015-08-14 77 views
1

我有两个表:优化缓慢更新查询

1)Testone - 具有100K行和1个索引和6非聚集索引

2)Testtwo - 具有316万行,1指数和4非聚集索引

我有update语句需要优化下列一个因执行该查询时,花更多的时间,你可以请帮我,最好的办法..

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 
+3

使用替换(或任何其他)函数的连接将执行可怕!您需要将您的表格加入到一个共同的索引列中。如果没有一个,那么你的查询将无法在数百万行中表现良好。 – beercohol

+3

也许添加一个[计算列](https://msdn.microsoft.com/en-us/library/ms188300.aspx)到testtwo而不是内联替换(替换。然后计算列可用于索引产生可观的结果 – xQbert

回答

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 

克利里替换(更换(mi.Model“ /',''),' - ','')将成为瓶颈。一个带有索引的计算列可能会做到这一点。