SQL Database Transaction Log is too big.

SQL Server数据库可以工作在三种不同的Recovery mode下。不同的Recovery mode决定了可能的数据丢失程度。SQL Server使用transaction-log来记录用户对数据的所有操作(包括DML和DDL,另外log中还有一些数据库自动发生的事件,但不包含查询语句)。因此当发生事务回滚等情况时,SQL Server通过transaction-log中记录的数据来回滚,其方式是执行log中记录操作的逆操作。另外Tlog在备份恢复,自动前滚和回滚中也有重要作用。 因此各个Recovery mode的区别本质就是在transaction-log中记录的数据内容的差别。

Full Mode 即事无巨细的在transaction-log中记录所有对数据有影响的操作,即所有的DML和DDL。在该模式下,为了防止log中的数据丢失,数据库不能自动删除或重利用log文件中的空间,即使当前事务已经完成,也不可以。 只有备份trancsation log,即log中的数据有保障之后,SQL server才能重新利用(truncate)log中的空间。因此在该模式下,TLog中的数据会持续增长,需要定时备份transation log来防止log增长的过大。如果log已经占用了过大空间则需要备份后(或手工truncate后),手工shink数据库。

Simple mode The “Simple” recovery model is the most basic recovery model for SQL Server.  Every transaction is still written to the transaction log, but once the transaction is complete and the data has been written to the data file the space that was used in the transaction log file is now re-usable by new transactions.  Since this space is reused there is not the ability to do a point in time recovery, therefore the most recent restore point will either be the complete backup or the latest differential backup that was completed. 因此simple模式下所记录的log一点都不比full模式的少。simple模式下transaction log比较小的原因是一旦事务完成,所占用的log空间就被标记为可覆盖的。 因此在simple模式下,数据库仍然支持事务。但是由于log可能很快就被覆盖,备份transaction log就没有意义了,因此无法对Tlog备份,也无法使用log将数据库恢复到某个时间点。当数据库重要度不高时,可以使用Simple模式,此时log文件增长较慢,便于管理。

Bulk logged Reduces log space usage by using minimal logging for most bulk operations. 因此当数据库处于该模式下时,对于大部分的批量操作不支持事务。但对于其他操作仍然支持事务,因此仍然需要备份transaction log来防止数据丢失。 也就是说在该模式下对批量操作是不记录log的。当数据库做批量操作时,可以考虑临时将数据库切换到该模式下。在做批量操作时,此模式会使磁盘IO最小。

原文地址:https://www.cnblogs.com/PeterHome/p/7457853.html