[英]optimize query performance for update
我有以下查詢:我有3.5億條記錄。 我如何優化查詢性能,以使其不花費4個小時?
DECLARE @Counter INT=0 --This causes the @@rowcount to be > 0
while @@rowcount>0
BEGIN
SET rowcount 10000
update r
set Comp=t.Comp
FROM [QlikDataWarehouse].[dbo].[Vente]r with (NOLOCK)
inner join [QlikDataWarehouse].[dbo].[Budget] t with (index (index_Budget ))
on t.[Code Site]=r.[Code Site]
and t.[Code Rayon] =substring(r.[Code Structure],1,4)
and t.[Date Time]=convert(date,r.[Date Time])
WHERE r.[Date Time] >= '2015-01-01 00:0:00.000'
end
SET rowcount 0
這是您的查詢:
update r
set Comp = t.Comp
from [QlikDataWarehouse].[dbo].[Vente]r with (NOLOCK) inner join
[QlikDataWarehouse].[dbo].[Budget] t)
on t.[Code Site] = r.[Code Site] and
t.[Code Rayon] = substring(r.[Code Structure],1,4) and
t.[Date Time] = convert(date,r.[Date Time])
where r.[Date Time] >= '2015-01-01 00:0:00.000';
優化將有些挑戰。 您的問題可能只是正在更新的記錄量。 如果是這樣,最好的方法是使用select into
重新創建表,截斷表,然后重新插入行。 發生這種情況是因為這些操作比更新所需的所有日志記錄和索引更新要快得多。
但是,索引可能會起作用。 您可能只是嘗試刪除index
提示,以查看SQL Server是否比優化時做得更好。 我認為最好的指標是: vente([Date Time], [Code Site], [Code Structure])
和Budget([Code Site], [Code Rayon], Date Time])
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.