[英]Large numbers of update performance
I have the following update statement below: 我在下面有以下更新声明:
UPDATE [dbo].[mytable]
SET [originaldate] =
CASE
WHEN [datecolumn1] <= [datecolumn2] THEN datecolumn3
ELSE [datecolumn1]
END
This statement executed for almost 5 minutes for 2.6 million of rows. 该语句执行了将近5分钟,执行了260万行。 Is there any way to optimize this query?
有什么方法可以优化此查询?
I tried converting the columns to date
like this CONVERT(date, [datecolumn2])
because the columns are datetime
but still the execution time is the same. 我试图转换的列
date
这样CONVERT(date, [datecolumn2])
因为列是datetime
,但仍执行时间是相同的。
Also, i tried putting the column and id that I needed in a temp table but still the performance is bad. 另外,我尝试将所需的列和ID放在临时表中,但性能仍然很差。
Why does it matter if it takes time? 为什么要花时间呢? This does not seem like something you should be doing regularly.
这似乎不应该定期执行。
If it is then you should consider using a computed column 如果是,则应考虑使用计算列
or even in application logic, a view or in the SELECT
statement depending on the situation. 甚至在应用程序逻辑中,根据情况在视图或
SELECT
语句中。
Updating all the rows in a table takes time because of logging and transaction overhead. 由于记录和事务开销,更新表中的所有行都需要时间。 You might find this faster:
您可能会发现更快:
select t.*
into #temp
from mytable;
truncate table mytable;
insert into mytable(col1, . . ., originaldate)
select . . .,
(CASE WHEN [datecolumn1] <= [datecolumn2] THEN datecolumn3
ELSE [datecolumn1]
END)
from #temp;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.