简体   繁体   English

SQL Server 中事务日志已满的实际原因

[英]Actual cause of full transaction log in SQL Server

I have a database (recovery mode = full), its transaction log is full.我有一个数据库(恢复模式=满),它的事务日志已满。 I know that I can solve it by shrinking the transaction log to fix the issue.我知道我可以通过缩小事务日志来解决这个问题。

But, I would like to know the reason why it became full?但是,我想知道它变满的原因是什么?

Since I read others and they said large amount of insertion & deletion can increase the transaction log size.因为我读过其他人,他们说大量的插入和删除会增加事务日志的大小。 But I cannot replicate the issue to reproduce the error of full transaction log (it somehow able to reallocate the space)但我无法复制问题来重现完整事务日志的错误(它以某种方式能够重新分配空间)

The questions are: How to simulate the issue of "transaction log full"?问题是:如何模拟“事务日志已满”的问题? How to check the physical transaction log?如何查看物理事务日志?

This query is applied to trace the current occupied log space but it is properly not the actual transaction log, as the occupied space of the query is trembling instead of static increase in the process of bulk insertion & deletion这个查询是用来跟踪当前占用的日志空间的,但它不是实际的事务日志,因为在批量插入和删除的过程中,查询的占用空间是在颤抖而不是静态增加

it is supposed that the log space will just increment as long as backup is not executed假设只要不执行备份,日志空间就会增加

DECLARE @TMPTBL AS TABLE (
DatabaseName nvarchar(500),
LogSize decimal(18,2),
LogSpaceUsed decimal (18,2),
[Status] int
) 

INSERT INTO @TMPTBL
EXEC ('DBCC SQLPERF(LOGSPACE)')

SELECT * FROM @TMPTBL WHERE DATABASENAME LIKE '%MYDBNAME%'
GO

There are multiple points to your question, so I will try to hit them individually:您的问题有多个要点,因此我将尝试分别针对它们进行分析:

Getting the size of the log: I prefer the following as it includes max size info.获取日志的大小:我更喜欢以下内容,因为它包含最大大小信息。 I am not saying never use SQLPERF, I am just adding another tool to the toolbox.我并不是说永远不要使用 SQLPERF,我只是在工具箱中添加了另一个工具。 Also realize there is a Disk Usage report in SSMS.还要意识到 SSMS 中有一个磁盘使用情况报告。

SELECT name, size/128 FileSizeInMB
, size/128 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128  AS EmptySpaceInMB
, iif (max_size = -1, null, (max_size)/128) AS MaxSize, iif(max_size > 0, cast(cast((cast(FILEPROPERTY(name, 'SpaceUsed') as decimal) /cast(max_size as decimal)*100) as decimal(18,2)) as varchar), '-') PctFilled
, file_id, type_desc, physical_name
FROM sys.database_files;

Checking what is in the transaction log.检查事务日志中的内容。 You should explore fn_dblog.您应该探索 fn_dblog。 Here is a link to a brief introduction https://logicalread.com/sql-server-dbcc-log-command-tl01/#.YAnC39hKiUk These queries should get you started.这是一个简要介绍的链接https://logicalread.com/sql-server-dbcc-log-command-tl01/#.YAnC39hKiUk这些查询应该可以帮助您入门。 It is an undocumented function, so use caution in production.这是一个未记录的功能,所以在生产中要小心。

SELECT *
FROM fn_dblog(null, null);

SELECT COUNT(1) as OperationCount,
    SUM (CAST([Log Record Length] as decimal)) as SumRecLen,
    [Operation]
FROM fn_dblog(null, null)
GROUP BY [Operation]    
ORDER BY 2 DESC;

Possible Causes: Checking the contents of the log will help you see where the load is coming from.可能的原因:检查日志的内容将帮助您了解负载来自何处。 Look at the operations with the high Log Record Length and dig on in. If you see the operation LOP_SHRINK_NOOP near the top of your list, you probably need to turn off Auto Shrink.查看具有高日志记录长度的操作并深入研究。如果您在列表顶部附近看到 LOP_SHRINK_NOOP 操作,则您可能需要关闭自动收缩。

How to simulate?如何模拟? I am guessing you mean trigger the 9002 error.我猜你的意思是触发 9002 错误。 I am not sure what this accomplishes but here is an approach.我不确定这会完成什么,但这是一种方法。

  1. Turn off auto-growth on the log.关闭日志的自动增长。
  2. Set the max size of your log to something small and just larger than the current size.将日志的最大大小设置为较小且仅大于当前大小的值。 You may need to backup/shrink your log file if a large amount of empty space is already allocated.如果已经分配了大量空白空间,您可能需要备份/缩小日志文件。
  3. Perform data manipulation until you hit the max size.执行数据操作,直到达到最大大小。 Consider a loop or a manual transaction that moves a large amount of data.考虑移动大量数据的循环或手动事务。 Example: Make a new physical table, insert 1000 integers into a it, then repeatedly update all values by 1.示例:新建一个物理表,向其中插入 1000 个整数,然后重复将所有值更新 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM