1

我使用 SQL Server 2008 R2,我的问题是使用名为 LogStats 的数据库。

如果我执行此查询:

select *
from ExceptionRow
     inner join HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
where ExceptionRow.Message is null
      AND not HashFP.MessageFP is null 

我有 126708 次点击,需要 2.05 分钟。但优化不是我的问题。

我想将数据从 HashFP.MessageFP 复制到 ExceptionRow.Message 而不覆盖任何数据。我试试这个:

UPDATE ExceptionRow
SET Exceptionrow.Message = HashFP.MessageFP
FROM ExceptionRow 
     INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL
      AND NOT HashFP.MessageFP IS NULL

结果:

消息 9002,级别 17,状态 4,第 1 行 数据库“LogStats”的事务日志已满。要找出日志中的空间不能被重用的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列

我试过这个:

SELECT name,log_reuse_wait_desc FROM sys.databases

从结果来看

tempdb   ACTIVE_TRANSACTION
LogStats ACTIVE_TRANSACTION

我怎样才能中止那些活动的事务,以便任务能够成功?

4

2 回答 2

1

我怎样才能中止那些活动的事务,以便任务能够成功?

你不能,因为这是UPDATE FROM交易。
您可以增加日志文件的最大大小:

ALTER DATABASE DB_NAME
MODIFY FILE (NAME=LOG_FILE_NAME,MAXSIZE=UNLIMITED);

或者你可以尝试这样的事情:

WHILE EXISTS
(select *
from ExceptionRow
     inner join HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
where ExceptionRow.Message is null
      AND not HashFP.MessageFP is null
)
UPDATE TOP (1000) ExceptionRow
SET Exceptionrow.Message = HashFP.MessageFP
FROM ExceptionRow 
     INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL
      AND NOT HashFP.MessageFP IS NULL

如果数据库具有简单的恢复模型,这应该可以工作,如果是 FULL 或 BULK_LOAD,您还需要在每次迭代中备份事务日志。

于 2013-10-25T06:26:37.940 回答
0
UPDATE ExceptionRow SET Exceptionrow.Message = HashFP.MessageFP FROM ExceptionRow 
INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL AND HashFP.MessageFP IS NOT NULL

或者

UPDATE ExceptionRow SET Exceptionrow.Message = HashFP.MessageFP FROM ExceptionRow 
INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL 

因为您将在使用HashFP.MessageFP为null 时更新ExceptionRow.Message ,所以我们无需担心 HashFP.MessageFP 中是否包含 null ..

于 2013-10-25T05:57:05.863 回答