[英]SQL Server Log full due to active transaction
I have been trying to update a column in a table and I am getting the below error:我一直在尝试更新表中的列,但出现以下错误:
The transaction log for database 'STAGING' is full due to 'ACTIVE_TRANSACTION'.
由于“ACTIVE_TRANSACTION”,数据库“STAGING”的事务日志已满。
I am trying to run the below statement :我正在尝试运行以下语句:
UPDATE [STAGING].[dbo].[Stg_Encounter_Alias]
SET
[valid_flag] = 1
FROM [Stg_Encounter_Alias] Stg_ea
where [ACTIVE_IND] = 1
and [END_EFFECTIVE_DT_TM] > convert(date,GETDATE())
My table has approx 18 million rows.我的表有大约 1800 万行。 And the above update will modify all the rows.
并且上面的更新会修改所有的行。 The table size is 2.5 GB.
表大小为 2.5 GB。 Also the DB is in simple recovery mode
数据库也处于简单恢复模式
This is something that I'll be doing very frequently on different tables.这是我会在不同的桌子上经常做的事情。 How can I manage this?
我该如何管理?
My Database size is as per below我的数据库大小如下
Below are the database properties!!!以下是数据库属性!!! I have tried changing the logsize to unlimited but it goes back to default.
我尝试将日志大小更改为无限制,但它又恢复为默认值。
Can any one tell me an efficient way to handle this scenario?谁能告诉我处理这种情况的有效方法?
If I run in batches :如果我分批运行:
begin
DECLARE @COUNT INT
SET @COUNT = 0
SET NOCOUNT ON;
DECLARE @Rows INT,
@BatchSize INT; -- keep below 5000 to be safe
SET @BatchSize = 2000;
SET @Rows = @BatchSize; -- initialize just to enter the loop
WHILE (@Rows = @BatchSize)
BEGIN
UPDATE TOP (@BatchSize) [STAGING].[dbo].[Stg_Encounter_Alias]
SET
[valid_flag] = 1
FROM [Stg_Encounter_Alias] Stg_ea
where [ACTIVE_IND] = 1
and [END_EFFECTIVE_DT_TM] > convert(date,GETDATE())
SET @Rows = @@ROWCOUNT;
END;
end
You are performing your update in a single transaction, and this causes the transaction log to grow very large.您在单个事务中执行更新,这会导致事务日志变得非常大。
Instead, perform your updates in batches, say 50K - 100K at a time.相反,请分批执行更新,例如一次 50K - 100K。
Do you have an index on END_EFFECTIVE_DT_TM
that includes ACTIVE_IND
and valid_flag
?您在
END_EFFECTIVE_DT_TM
上是否有包含ACTIVE_IND
和valid_flag
的索引? That would help performance.这将有助于表现。
CREATE INDEX NC_Stg_Encounter_Alias_END_EFFECTIVE_DT_TM_I_
ON [dbo].[Stg_Encounter_Alias](END_EFFECTIVE_DT_TM)
INCLUDE (valid_flag)
WHERE ([ACTIVE_IND] = 1);
Another thing that can help performance drastically if you are running Enterprise Edition OR SQL Server 2016 SP1 or later (any edition), is turning on data_compression = page
for the table and it's indexes.如果您运行的是 Enterprise Edition OR SQL Server 2016 SP1 或更高版本(任何版本),另一件可以显着提高性能的事情是为表及其索引打开
data_compression = page
。
Thank for your help Mitch. 谢谢你的帮助米奇。 I have actually increased the transaction log size and removed all the indexes as its a staging DB.
我实际上已经增加了事务日志大小并删除了所有索引作为其临时数据库。 And its updating 17 million rows in 24s :)
它在24s内更新了1700万行:)
Though I am not very happy with the solution but due to lack of time I am going further with this. 虽然我对解决方案不是很满意,但由于时间不够,我会更进一步。 I'll have to work further on the efficiency.
我将不得不进一步研究效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.