SQL Server事务日志增长监视和管理

In the previous articles of this series on the SQL Server Transaction Log, we discussed the importance of the SQL Server Transaction Log and the role that it plays in maintaining the database consistency, by ensuring that the committing transactions data will be preserved and the failed transaction will be rolled back. It also helps to recover the database to a specific point in time in case of system or hardware failure. This is achieved by writing a log record to the SQL transaction log file before writing the data pages to the physical data file, using Write-ahead Logging process.

在本系列有关SQL Server事务日志的前几篇文章中,我们讨论了SQL Server事务日志的重要性及其在通过确保将保留提交的事务数据和失败的事务而保持数据库一致性方面所起的作用。将回滚。 如果系统或硬件出现故障,它还有助于将数据库恢复到特定的时间点。 这是通过使用预写日志记录过程在将数据页写入物理数据文件之前将日志记录写入SQL事务日志文件来实现的。

In addition, we discussed deeply the three Full, Simple and Bulk-Logged recovery models of the database, that specifies how these transactions will be logged and saved to the SQL Server transaction log file. In this article, we will see how to monitor the SQL Server Transaction Log growth and how to manage that growth.

此外,我们深入讨论了数据库的三种完整,简单和批量记录的恢复模型 ,这些模型指定了如何记录这些事务并将其保存到SQL Server事务日志文件中。 在本文中,我们将看到如何监视SQL Server事务日志的增长以及如何管理该增长。

SQL事务日志增长 (SQL Transaction Log Growth)

The SQL Server Database Engine writes a log record for every single operation in the database, that includes starting or ending a SQL transaction when a data modification process is performed, when a database table or index is created or dropped, and after each page allocation or deallocation operation. In the heavily transactional systems with excessive logging operations, the SQL Server Transaction Log file will grow rapidly, until it reaches its maximum size, generating error number 9002. And if the auto-growth option is enabled, the underlying disk drive will run out of free space.

SQL Server数据库引擎为数据库中的每个操作写入一条日志记录,包括在执行数据修改过程时,在创建或删除数据库表或索引时以及在每次页面分配或执行之后开始或结束SQL事务。释放操作。 在具有过多日志记录操作的高事务性系统中,SQL Server事务日志文件将快速增长,直到达到最大大小,并生成错误号9002 并且,如果启用了自动增长选项,则基础磁盘驱动器将耗尽可用空间。

Before going through the mechanisms that can be used to monitor and manage SQL Server Transaction Log growth, let us discuss the excessive logging operations that may lead the SQL Transaction Log file to grow rapidly and the operations that lock the transaction and prevent it from being reused by other transactions.

在探讨可用于监视和管理SQL Server事务日志增长的机制之前,让我们讨论可能导致SQL事务日志文件快速增长的过多日志记录操作,以及锁定事务并防止其被重用的操作。通过其他交易。

索引重建和重组 (Index Rebuild and Reorganize )

SQL Server index rebuild operation is a fully logged operations that use the Transaction Log excessively, especially when the recovery model of the database is FULL. It runs by default in a single long-running transaction, that prevents the Transaction Log space reuse while rebuilding a large index. If the recovery model of the database is changed to Simple or Bulk-Logged, the index rebuild operation will be a minimally logging operation.

SQL Server索引重建操作是完全记录的操作,过度使用了事务日志,尤其是在数据库的恢复模型为FULL时。 默认情况下,它在单个长时间运行的事务中运行,从而防止了在重建大型索引时重复使用事务日志空间。 如果将数据库的恢复模型更改为“简单”或“批量记录”,则索引重建操作将是最少记录操作。

The case in the index reorganizes operation is not the same. The index reorganizes operation is always fully logged, regardless of the recovery model setting of the database. You can imagine the SQL Transaction Log space that is required to reorganize a large index that is heavily fragmented. In all cases, reorganizing an index with a specific size requires less log space than rebuilding the same index.

索引重组操作的情况是不同的。 无论数据库的恢复模型设置如何,索引重组操作始终被完全记录。 您可以想象重新组织散乱的大索引所需SQL事务日志空间。 在所有情况下,与重建相同索引相比,重组具有特定大小的索引所需的日志空间更少。

SQL事务日志备份 (SQL Transaction Log Backup)

When the recovery model of the database is FULL or Bulk-Logged, which is the default recovery model of all newly created databases, unless you change the recovery model setting in the model system database, the inactive part of the SQL Server Transaction Log file that is waiting for truncation will not be truncated automatically unless a Transaction Log backup is performed on that database to explicitly truncate that part of the log. It is a common mistake to configure only Full backup plan on the database with the Full recovery model, as the Full backup process will not truncate the SQL transaction log file and make it available for reuse. In this case, the log file will grow continuously, without truncation, until it runs out of free space.

当数据库的恢复模型为FULLBulk-Logged时 ,这是所有新创建的数据库的默认恢复模型,除非您更改模型系统数据库中的恢复模型设置,否则SQL Server事务日志文件的非活动部分将除非在该数据库上执行事务日志备份以显式截断日志的该部分,否则正在等待截断的消息将不会被自动截断。 一个常见的错误是在具有完全恢复模型的数据库上仅配置完全备份计划,因为完全备份过程不会截断 SQL事务日志文件并使它可供重用。 在这种情况下,日志文件将连续增长,而不会被截断,直到耗尽可用空间为止。

有效交易 (Active Transactions)

A SQL Server Transaction is a single unit of work consists of multiple related tasks that should succeed or fail as one atomic unit. A long-running or uncommitted transaction, such as archiving and purging transactions, delays the truncation of VLFs that contain the log records related to that active transaction, even when the recovery model of the database is Simple, due to the fact that the VLF will not be truncated if it contains any active log record.

SQL Server事务是一个工作单元,由多个相关任务组成,这些任务应该作为一个原子单元成功或失败。 长时间运行或未提交的事务(例如归档和清除事务)会延迟包含与该活动事务相关的日志记录的VLF的截断,即使数据库的恢复模型很简单,由于VLF会如果包含任何活动日志记录,则不会被截断。

To overcome the uncommitted transaction issue, make sure to write complete transactions that are handled in case of success or failure and monitor the orphaned distributed transactions that stuck without being committed. On the other hand, you can overcome the long-running transaction issue by breaking down the long transaction into smaller transactions that work in batches. For example, you can put the DELETE statement inside a loop, that keeps searching for old data and delete it in batches.

为了克服未提交的事务问题,请确保编写成功或失败时要处理的完整事务,并监视卡住的未提交的孤立分布式事务。 另一方面,您可以通过将长期交易分解为可批量处理的较小交易来克服长期交易问题。 例如,您可以将DELETE语句放入循环中,该循环将继续搜索旧数据并将其批量删除。

SQL Server复制和更改数据捕获 (SQL Server Replication and Change Data Capture)

SQL Server Replication is a database level, high availability solution consists of a publisher server, that distributes a list of database tables to one or more subscriber servers, used for read-only reporting operations. On the other hand, Change Data Capture is a SQL Server feature used to track and capture the DML changes that are performed on the SQL Server database tables.

SQL Server复制是数据库级别的高可用性解决方案,包括 发布服务器 ,将数据库表的列表分发到一个或多个订户服务器,用于只读报告操作。 另一方面,“ 更改数据捕获”是一种SQL Server功能,用于跟踪和捕获对SQL Server数据库表执行的DML更改。

What is common between these two features is that both features depend on the log reader agent to read the active part of the SQL Server Transaction Log associated with the changes to be replicated. These SQL Transaction Log records will be in pending replication state until these tracked changes are replicated to the subscriber database, in case of replication, or to the tracking tables in case of CDC.

这两个功能之间的共同点是,这两个功能都依赖于日志读取器代理来读取与要复制的更改关联SQL Server事务日志的活动部分。 这些SQL事务日志记录将处于挂起复制状态,直到将这些跟踪的更改复制到订户数据库(如果有复制)或复制到跟踪表(如果有CDC)。

If the subscriber database is not reachable, the SQL Server Agent jobs is in stop state, the Agent jobs associated with the replication process is not working or simply the log agent reader activities are slow, all SQL Server Transaction Log records that are not replicated yet will stay part of the active log in pending replication state. Again, having these log records in an active state for a long time will prevent the VLFs that contain these log records from being truncated, causing the Transaction Log file to grow continuously until it runs out of free space.

如果无法访问订户数据库,则SQL Server代理作业处于停止状态,与复制过程关联的代理作业无法正常工作,或者仅仅是日志代理读取器活动缓慢,所有尚未复制SQL Server事务日志记录将在挂起的复制状态下保留在活动日志中。 同样,使这些日志记录长时间处于活动状态将防止包含这些日志记录的VLF被截断,从而导致事务日志文件持续增长,直到其可用空间用尽。

You can troubleshoot this issue by making sure that the SQL Server Agent Service is running, the Agent jobs associated with the replication and CDC are running and finally that the subscriber server is reachable.

您可以通过确保SQL Server代理服务正在运行,与复制和CDC关联的代理作业正在运行以及最终可以访问订阅服务器来解决此问题。

数据库镜像和始终可用性组 (Database Mirroring and Always on Availability Group)

SQL Server Database Mirroring is a database level high availability solution, that consists of a principal server that sends the active Transaction Log records to the mirror database in the mirror server then redoes these changes on the mirror database. On the other hand, the SQL Server Always on Availability Group feature, is a database level high availability solution, that consists of a primary server, that sends the transaction log records from the primary databases to the secondary database, to be written in the database transaction log file first for caching, and reflect the changes later to the database.

SQL Server 数据库镜像是数据库级的高可用性解决方案,由主体服务器组成,该主体服务器将活动的事务日志记录发送到镜像服务器中的镜像数据库,然后在镜像数据库上重做这些更改。 另一方面,SQL Server 始终可用性组功能是数据库级的高可用性解决方案,它由一个主服务器组成,该服务器将事务日志记录从主数据库发送到辅助数据库,以写入数据库中事务日志文件首先用于缓存,然后将更改反映到数据库中。

If the Database Mirroring or the Always on Availability Group is configured to work in synchronous mode, the transaction will be committed only when the SQL Server transaction log is written to the secondary server. When working in asynchronous mode, the transaction will be committed without waiting for it to be committed in the secondary site. But in all cases, the log record will not be marked as inactive until it is copied to the secondary site.

如果将数据库镜像或始终可用组配置为在同步模式下工作,则仅在将SQL Server事务日志写入辅助服务器时才提交事务。 在异步模式下工作时,将提交事务,而无需等待在辅助站点中提交事务。 但是在所有情况下,除非将日志记录复制到辅助站点,否则不会将其标记为非活动状态。

If the mirrored server or secondary replica is not reachable, the SQL Transaction Log records in the primary site that are not copied to the secondary site yet will remain part of the active log that cannot be truncated. After a long time of disconnection, the number of SQL transaction logs pending the mirroring or the availability replica will increase, leading the SQL Server Transaction Log file to run out of free space due to not being able to reuse the log space.

如果无法访问镜像服务器或辅助副本,则主站点中SQL事务日志记录(未复制到辅助站点)仍将保留为活动日志的一部分,不能被截断。 长时间断开连接后,等待镜像或可用性副本SQL事务日志的数量将增加,由于无法重用日志空间,导致SQL Server事务日志文件用完可用空间。

To overcome this issue, make sure that you troubleshoot and fix the connectivity issue between the replicas, or the issue that caused the mirroring or the availability group sites to fail as soon as possible, in order not to have a large number of records pending in the primary site.

要解决此问题,请确保对副本之间的连接问题或引起镜像或可用性组站点故障的问题尽快进行故障排除和修复,以免有大量记录挂起。主站点。

长时间运行的备份 ( Long Running Backup)

When a database Full or Differential backup operation is running, SQL Server Transaction Log truncation will be delayed, in order to include the active portion of the Transaction Log in that backup. If this backup operation keeps running for a long time, the SQL Transaction Log truncation will be delayed for a longer time and the SQL Transaction Log file will grow, due to not being able to reuse the inactive part of the log.

当数据库完全备份或差异备份运行时,SQL Server事务日志截断将被延迟,以便在该备份中包括事务日志的活动部分。 如果此备份操作保持长时间运行,则由于无法重用日志的非活动部分,SQL事务日志截断将被延迟更长的时间,并且SQL事务日志文件将增长。

You need to troubleshoot the main cause behind running the log operation for a long time, such as the internal blocking process or a performance issue on the underlying disk I/O system to overcome that log growth.

您需要解决长时间运行日志操作背后的主要原因,例如内部阻塞过程或基础磁盘I / O系统上的性能问题,以克服该日志增长的问题。

对日志增长进行故障排除 (Troubleshoot Log Growth)

When the SQL Server Transaction Log file of the database runs out of free space, you need first to verify the Transaction Log file size settings and check if it is possible to extend the log file size. If you are not able to extend the log file size and the database recovery model is Full, you can force the log truncation by changing it to Simple recovery model. If the database recovery model is already Simple or changing it to Simple recovery model is not applicable, you need to identify what is preventing the SQL Server Transaction Log from being truncated.

当数据库SQL Server事务日志文件用完可用空间时,首先需要验证事务日志文件的大小设置,并检查是否可以扩展日志文件的大小。 如果您无法扩展日志文件的大小,并且数据库恢复模型为“完全”,则可以通过将其更改为“简单”恢复模型来强制截断日志。 如果数据库恢复模型已经很简单,或者将其更改为“简单”恢复模型不适用,则需要确定阻止SQL Server事务日志被截断的原因。

The sys.databases system catalog view can be used to retrieve useful information about the reason behind preventing the SQL Transaction Log file from being truncated, under the log_reuse_wait_desc column, as in the query below:

sys.databases系统目录视图可用于在log_reuse_wait_desc列下检索有关防止SQL事务日志文件被截断的原因的有用信息,如以下查询中所示:

querying the sys.databases to get the log_reuse information

The result returned from the previous query will be like the below:

从上一个查询返回的结果将如下所示:

SQL Server事务日志增长监视和管理_第1张图片

The log_reuse_wait_desc column value that indicates a healthy state for the database Transaction Log file is NOTHING, which means that the SQL Transaction Log is truncated and reusable. Other values that may return from the sys.databases in the log_reuse_wait_desc column, that shows what is preventing the SQL Transaction Log from being truncated, include:

指示数据库事务日志文件的健康状态的log_reuse_wait_desc列值是NOTHING ,这意味着SQL事务日志已被截断并且可以重用。 可能从log_reuse_wait_desc列中的sys.databases返回的其他值(表明阻止SQL事务日志被截断的原因)包括:

  • CHECKPOINT

    检查点
  • LOG_BACKUP

    LOG_BACKUP
  • ACTIVE_BACKUP_OR_RESTORE

    ACTIVE_BACKUP_OR_RESTORE
  • ACTIVE_TRANSACTION

    ACTIVE_TRANSACTION
  • DATABASE_MIRRORING

    DATABASE_MIRRORING
  • REPLICATION

    复制
  • DATABASE_SNAPSHOT_CREATION

    DATABASE_SNAPSHOT_CREATION
  • LOG_SCAN

    LOG_SCAN
  • AVAILABILITY_REPLICA

    AVAILABILITY_REPLICA

After identifying the reason behind preventing the SQL Transaction Log from being truncated, you can troubleshoot that blocker as discussed earlier in this article. For more information about managing the SQL Transaction Log, check Managing SQL Server transaction logs.

在确定了防止SQL事务日志被截断的背后原因之后,您可以按照本文前面的讨论对阻止程序进行故障排除。 有关管理SQL事务日志的更多信息,请检查管理SQL Server事务日志 。

监视SQL Server事务日志文件 (Monitoring SQL Server Transaction Log File)

It is always better to be a proactive database administrator and keep an eye on the SQL Server Transaction Log file growth, in order to prevent catastrophic issues when having the log file running out of free space for a long time. Rather than sleeping beside the server, you can use a monitoring tool such as the System Center Operations Manager (SCOM) tool, Performance Monitor counters, or simply create an alert that reads from one of the system catalog views and notify an operator by email when the free space of the SQL Transaction Log file becomes under a predefined threshold.

最好是积极主动的数据库管理员并注意SQL Server事务日志文件的增长,以防止日志文件长时间耗尽可用空间时发生灾难性问题。 您可以使用诸如System Center Operations Manager(SCOM)工具,Performance Monitor计数器之类的监视工具,而不是在服务器旁边睡觉,或者简单地创建一个从系统目录视图之一读取的警报 ,并在出现以下情况时通过电子邮件通知操作员SQL事务日志文件的可用空间将低于预定义的阈值。

sys.dm_db_log_space_usage is a dynamic management view, introduced in SQL Server 2012, that is used to return space usage information for the transaction log. The below query can be used to check the free space percentage in the SQL Transaction Log file of the current database:

sys.dm_db_log_space_usage是SQL Server 2012中引入的动态管理视图,用于返回事务日志的空间使用情况信息。 以下查询可用于检查当前数据库SQL事务日志文件中的可用空间百分比:

Log File Free Space

And the result will be like:

结果将是:

sys.dm_db_log_space_usage result

If the result returned from the previous query falls down a predefined threshold, before running the SQL Server Transaction Log file out of free space, the DBA will be notified by an email, SMS or call based on the monitoring tool used in your entity.

如果前一个查询返回的结果下降到预定义的阈值,则在SQL Server事务日志文件中的可用空间不足之前,将通过电子邮件,SMS或基于您的实体使用的监视工具的呼叫来通知DBA。

摘要 (Summary)

In the next article of this series, we will discuss the different operations that can be performed on the SQL Transaction Log including the back, truncate and shrink operations, and make it easier for the reader to identify one from the other. Stay tuned!

在本系列的下一篇文章中,我们将讨论可以在SQL事务日志上执行的不同操作,包括后退,截断和收缩操作,并使读者更容易识别彼此。 敬请关注!

目录 (Table of contents)

SQL Server Transaction Overview
SQL Server Transaction Log Architecture
What are SQL Virtual Log Files aka SQL Server VLFs?
SQL Server Transaction Log and Recovery Models
SQL Server Transaction Log and High Availability Solutions
SQL Server Transaction Log Growth Monitoring and Management
SQL Server Transaction Log Backup, Truncate and Shrink Operations
SQL Server Transaction Log Administration Best Practices
Recovering Data from the SQL Server Transaction Log
How to Rebuild a Database with a Corrupted or Deleted SQL Server Transaction Log File
Auditing by Reading the SQL Server Transaction Log
SQL Server事务概述
SQL Server事务日志体系结构
什么是SQL虚拟日志文件(又名SQL Server VLF)?
SQL Server事务日志和恢复模型
SQL Server事务日志和高可用性解决方案
SQL Server事务日志增长监视和管理
SQL Server事务日志备份,截断和缩减操作
SQL Server事务日志管理最佳实践
从SQL Server事务日志中恢复数据
如何使用损坏或删除SQL Server事务日志文件重建数据库
通过读取SQL Server事务日志进行审核

翻译自: https://www.sqlshack.com/sql-server-transaction-log-growth-monitoring-and-management/

你可能感兴趣的:(数据库,mysql,java,python,oracle)