优化更新查询

时间:2015-10-15 12:45:39

标签: sql sql-server

我有以下查询。我期待更新超过3.5亿行

update t   
set [OC]=r.[OC]

from [dbo].[Vente MPX] t  with (index (idocdate )) 

INNER join [dbo].tempOCMPX r with (index (idoc))  

on t.[Date Time] between r.[DATE DEBUT] and r.[DATE FIN]

--WHERE convert(date,t.[Date Time]) >= '2015-06-01'

--AND convert(date,t.[Date Time]) <= '2015-08-31'

WHERE t.[Date Time] >= '2015-01-01 00:0:00.000'

--AND r.[DATE DEBUT] <= '2015-08-31'

and t.[OC] is null

这里是执行计划 enter image description here enter image description here 此查询已执行4天以上,更新的行数为6,000万行。 我该如何优化呢?

1 个答案:

答案 0 :(得分:0)

当您需要更新数百万条记录时,我建议您按照以下步骤操作:

  1. 将一个更新语句拆分为多个短事务。创建一个循环并一次更新1万 - 1百万条记录(选择金额)。您可以按日期限制更新查询:从$('input[type=number]').val(function (index, currentValue) { // sets the new value of each number-<input> to the // result of the parsed current-value multiplied by // the index of the current element in the collection // returned by the selector: return parseInt(currentValue, 10) * index; }); 开始,每次迭代开始一天/周/月并更新数据
  2. 您可以在更新前删除索引,并在完成所有更新后创建索引。毕竟构建索引比在更新期间重建它更快。