10个最重要SQL Server事务日志神话

Myth: SQL transaction log truncation will make it smaller 误解: SQL事务日志截断将使其变小

The truncation process does not reduce the size of a physical log file

截断过程不会减少物理日志文件的大小

During the truncation process, only the active portion of the online SQL Server transaction log file is scanned. Some parts of the scanned portion are marked as inactive and they will be used as free space to write down new transactions. There isn’t a change in the online transaction log size because the inactive parts remain intact, nothing is deleted or removed

在截断过程中,仅扫描联机SQL Server事务日志文件的活动部分。 扫描部分的某些部分被标记为非活动状态,它们将被用作空闲空间以记下新事务。 联机事务日志大小没有变化,因为不活动的部分保持不变,没有任何内容被删除或删除

Every SQL Server transaction log is made of Virtual Log Files (VLFs). During the truncation process, only the Logical log is scanned. A Logical log is made of active VLFs. A Log Sequence Number (LSN) is used to uniquely identify every transaction in the online transaction log. The MinLSN is the starting point of the oldest active transaction in the online transaction log

每个SQL Server事务日志均由虚拟日志文件(VLF)组成。 在截断过程中,仅扫描逻辑日志。 逻辑日志由活动的VLF组成。 日志序列号(LSN)用于唯一标识联机事务日志中的每个事务。 MinLSN是在线事务日志中最早的活动事务的起点

The online SQL Server transaction log file is circular by internal organization. When logging reaches the end of the transaction log, it starts again from the beginning by overwriting the parts marked as inactive

联机SQL Server事务日志文件由内部组织发出。 当日志记录到达事务日志的末尾时,它会通过覆盖标记为非活动的部分从头开始重新开始

SQL Server transaction log contents and organization

Orange represents the Logical log, blue the truncated part of the online transaction log ready to be overwritten

橙色代表逻辑日志,蓝色代表在线交易日志的截断部分,随时可以覆盖

Myth: Having multiple online SQL Server transaction log files will increase performance

误解:拥有多个联机SQL Server事务日志文件将提高性能

This myth is based on the belief that having multiple online transaction log files will result in parallel writing of transactions into the files and therefore result in performance gain. SQL Server can’t operate with more than one online transaction log file at the time, so any kind of parallel I/O is not possible

这个神话基于这样的信念,即拥有多个联机事务日志文件将导致将事务并行写入文件中,从而提高性能。 SQL Server当时不能使用多个在线事务日志文件,因此任何类型的并行I / O都是不可能的

Having multiple transaction log files is needed only in the situations where the initial SQL Server transaction log can’t record more transactions due to a lack of free space on the disk

仅在初始SQL Server事务日志由于磁盘上的可用空间不足而无法记录更多事务的情况下才需要具有多个事务日志文件

Myth: The SQL Server transaction log won’t grow if the database is in the Simple recovery model

误解:如果数据库处于简单恢复模型中,则SQL Server事务日志将不会增长

However, it happens just in some specific situations – when there is a long running transaction or transaction that creates many changes

但是,它仅在某些特定情况下发生–当长时间运行的事务或产生许多更改的事务

In the Simple recovery model, the online transaction log is cleared automatically. SQL Server automatically reclaims log space to keep space requirements small – but that doesn’t mean it won’t grow. The online transaction log must provide enough information for a database rollback, therefore it must provide enough space for all necessary information. As all transactions must be written into the online transaction log, in case of a large number of changes in a transaction, there might not be enough space in the log, so it must be expanded

在简单恢复模型中,将自动清除联机事务日志。 SQL Server自动回收日志空间以减小空间需求–但这并不意味着它不会增长。 联机事务日志必须为数据库回滚提供足够的信息,因此它必须为所有必要的信息提供足够的空间。 由于所有事务都必须写入在线事务日志中,因此如果事务中发生大量更改,则日志中可能没有足够的空间,因此必须对其进行扩展

Myth: A SQL Server transaction log backup will be the same size as the online transaction log itself

误解: SQL Server事务日志备份的大小将与联机事务日志本身的大小相同

The online transaction log must have enough information to rollback active transactions, so some space is reserved for eventual rollbacks. If a rollback occurs, SQL Server doesn’t want to expand the online transaction log because if the expanding fails, the SQL Server database can become inconsistent or go into the Suspect mode. That’s why the online transaction log has some reserved space and is usually bigger than the SQL Server transaction log backup

联机事务日志必须具有足够的信息来回滚活动事务,因此保留一些空间以供最终回滚。 如果发生回滚,则SQL Server不想扩展联机事务日志,因为如果扩展失败,则SQL Server数据库可能会变得不一致或进入“可疑”模式。 这就是为什么联机事务日志具有一些保留空间并且通常大于SQL Server事务日志备份的原因

Moreover, a transaction log backup contains only the transactions made after the last transaction log backup. If the online transaction log contains the transactions that have already been backed up, they will not be present in the new SQL Server transaction log backup, therefore the transaction log backup will be smaller for that amount of space

此外,事务日志备份仅包含上次事务日志备份之后进行的事务。 如果联机事务日志包含已备份的事务,则它们将不会出现在新SQL Server事务日志备份中,因此,在该空间量下,事务日志备份将较小。

Myth: A full or differential database backup clears the online transaction log

误解:完整或差异数据库备份会清除联机事务日志

The inactive parts of the online SQL Server transaction log are marked for clearing only when a transaction log backup is created

联机SQL Server事务日志的非活动部分被标记为仅在创建事务日志备份时才清除

Full and differential database backups don’t contain much SQL Server transaction log information, only the transactions necessary to recover the database into a consistent state. These transactions are not a backup of the online transaction log, therefore these transactions are not marked for overwriting in the online transaction log

完整和差异数据库备份不包含很多SQL Server事务日志信息,仅包含将数据库恢复到一致状态所需的事务。 这些事务不是联机事务日志的备份,因此这些事务未在联机事务日志中标记为覆盖

Myth: The TRUNCATE TABLE and DROP TABLE commands are not logged into the online transaction log

误解: TRUNCATE TABLE和DROP TABLE命令未登录到在线事务日志中

The exact deleted values are not logged in the online SQL Server transaction log, only the IDs of the pages that held the truncated records are logged. These pages are marked for overwriting in the database data file and the truncated data will be gone for good when the new transactions are written to these pages

确切的删除值不会记录在联机SQL Server事务日志中,仅记录保存了截断记录的页面的ID。 这些页面在数据库数据文件中标记为要覆盖,并且将新事务写入这些页面时,被截断的数据将永久消失

This myth is also based on the fact that these commands take little time to execute, they are almost instantaneous

这个神话还基于以下事实:这些命令几乎不需要执行时间,它们几乎是瞬时的。

Myth: My SQL Server is too busy, I don’t want to make SQL Server transaction log backups

误解:我SQL Server太忙了,我不想进行SQL Server事务日志备份

One of the biggest performance intensive operations in SQL Server is an auto-grow event of the online transaction log file. By not making transaction log backups often enough, the online transaction log will become full and will have to grow. The default growth size is 10%. The busier the database is, the quicker the online transaction log will grow if transaction log backups are not created

SQL Server中最大的性能密集型操作之一是联机事务日志文件的自动增长事件。 由于没有足够频繁地进行事务日志备份,因此联机事务日志将变满并且必须增长。 默认增长大小为10%。 数据库越忙,如果未创建事务日志备份,则在线事务日志将越快增长

Creating a SQL Server transaction log backup doesn’t block the online transaction log, but an auto-growth event does. It can block all activity in the online transaction log

创建SQL Server事务日志备份不会阻止联机事务日志,但是会自动增长事件。 它可以阻止在线交易日志中的所有活动

Myth: A SQL Server transaction log backup isn’t needed for a point in time restore. A full database backup is enough

误解:时间点还原不需要SQL Server事务日志备份。 完整的数据库备份就足够了

This myth comes from using the RESTORE command with STOPAT clause to restore from a full database backup. The STOPAT clause specifies a point in time for the RESTORE LOG command, and it works well when it’s used with a transaction log backup. The fact that it can be used with a full database backup makes you believe that transaction log backups are not needed to recover to a specific point in time

这个神话来自将RESTORE命令和STOPAT子句一起使用来从完整的数据库备份中还原。 STOPAT子句为RESTORE LOG命令指定一个时间点,当它与事务日志备份一起使用时,它可以很好地工作。 它可以与完整的数据库备份一起使用,这一事实使您相信不需要将事务日志备份恢复到特定的时间点

An example of T-SQL code for restoring the AdventureWorks database to December 31st 2013 10:59 PM

用于将AdventureWorks数据库还原到2013年12月31日晚上10:59的T-SQL代码示例

RESTORE DATABASE AdventureWorks FROM DISK = 'D:\AdventureWorks.bak'
WITH NORECOVERY
GO
RESTORE LOG AdventureWorks FROM DISK = 'D:\AdventureWorks.bak'
WITH RECOVERY, 
STOPAT = 'Dec 31, 2013 10:59:00 PM'
GO

Although the SQL Server database cannot be restored to a point in time, SQL Server doesn’t clearly identify the problem, and it allows you to use the STOPAT clause without a transaction log backup specified

尽管无法将SQL Server数据库还原到某个时间点,但是SQL Server不能清楚地确定问题所在,它允许您使用STOPAT子句而无需指定事务日志备份

RESTORE DATABASE successfully processed 24436 pages in 5.498 seconds (34.722 MB/sec). This backup set contains records that were logged before the designated point in time. The database is being left in the restoring state so that more roll forward can be performed. RESTORE LOG successfully processed 4 pages in 0.088 seconds (0.338 MB/sec).

RESTORE DATABASE在5.498秒(34.722 MB /秒)中成功处理了24436页。 该备份集包含在指定时间点之前记录的记录。 数据库处于恢复状态,以便可以执行更多前滚。 RESTORE LOG在0.088秒(0.338 MB /秒)内成功处理了4页。

Myth: SQL Server transaction log backups are not needed for successful disaster recovery if the full database backup is taken daily

误解:如果每天进行完整的数据库备份,则无需SQL Server事务日志备份即可成功进行灾难恢复

It also depends on how much data you can lose. If you can afford to lose up to 24 hours of data, then you don’t need transaction log backups and you should use the Simple recovery model

它还取决于您可能丢失多少数据。 如果您有能力丢失多达24小时的数据,则不需要事务日志备份,而应使用简单恢复模型

If the information you can lose is measured in minutes and hours, regular transaction log backups are necessary, as the maximum you will lose is the time between to transaction log backups

如果以分钟和小时为单位衡量您可能丢失的信息,则必须进行定期的事务日志备份,因为您丢失的最大时间是两次事务日志备份之间的时间

Myth: The SQL Server transaction log shrinking will make free space in the online transaction log so I don’t need to create transaction log backups

误解: SQL Server事务日志的缩小将在联机事务日志中释放可用空间,因此我无需创建事务日志备份

The shrink operation is not a good maintenance practice because it doesn’t solve the transaction log size issue permanently. After the initial shrinking, the transaction log will grow again. As the auto-growth event is one of the most intensive SQL Server operations, it should be avoided. The recommended method to keep the size of the online transaction log is to create transaction log backups regularly. Or, switching to the Simple recovery model, if you can tolerate data loss

收缩操作不是良好的维护习惯,因为它不能永久解决事务日志大小问题。 初始收缩后,事务日志将再次增长。 由于自动增长事件是SQL Server最密集的操作之一,因此应避免使用它。 保持联机事务日志大小的建议方法是定期创建事务日志备份。 或者,如果可以忍受数据丢失,则切换到简单恢复模型

翻译自: https://www.sqlshack.com/10-important-sql-server-transaction-log-myths/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值