How MSSQL shrinks log files

 

 

  The log file (*.ldf) of the database is getting bigger and bigger, what should I do?

  Shrink it. The operation of shrinking log files is not simple, and it is also related to the recovery mode of the database.

 

1. Log shrinking in the "Simple Recovery Model"

1. Truncate the log

  When the recovery mode of the database is "simple", the log file will be truncated in the following cases:
(1) Full backup

(2) Encounter a checkpoint (checkpoint)

wKiom1Zo0n6Bzv_mAABKgjhelIc683.png

 

2. Manually shrink the log file

  When the log is truncated, the internal space of the log file is marked as "reusable", so the log file does not need to keep growing. For an explanation of the log file space status, see  http://jimshu.blog.51cto.com/3171847/1174474 

  The operation of manually shrinking the log file can refer to the instructions later in this article.

 

 

Second, the "full recovery model" log shrinkage

  Under the full recovery model, a full backup of data or a checkpoint will not affect the log. Therefore, it may result that the log file cannot be truncated, that is, the space cannot be recycled. As a result, the log file will continue to grow, even approaching the limit of hard disk space. At this point, even if you shrink the log file manually, it will not reduce the file space because the space is all occupied.

  Note: The "Bulk-Logged Recovery Model" and "Full Recovery Model" have the same log processing method.

 

1. Confirm Recovery Mode

  Below is the recovery model to check the database using SQL Server Management Studio.

174856565.png

 

 

2. Full backup

  Before log backup, a full backup must be done.

175721601.png

 

Note: Do not tick the "Copy backups only" option. "Copy Backup Only" does not affect the log.

 

3. Transaction log backup

  Transaction log backups can be performed at any time after a full backup has been made.

180019793.png

 

  The default option is to truncate the transaction log after backup.

180532340.png

 

Note : There are many situations that prevent log truncation, not having a backup transaction log is just one of those situations, and the most common.

 

4. Manually shrink the log file

  After a log backup is made, the log is truncated and a lot of space is marked as "reusable". Under normal circumstances, the "shrink file" operation can be performed at this time to remove the "reusable" space from the log file, thereby reducing the log file.

181442401.png

 

 

3. Unofficial methods of operation

1. Temporarily switch to Simple Recovery Mode

  Temporarily change to simple recovery mode, then do a full backup, or run checkpoint to perform a checkpoint. When the log is truncated, the log file is then shrunk, and finally changed back to the full recovery model.

  Even in the simple recovery mode, you can directly modify the "initial size" of the log file to a small value (the system will automatically shrink the log file to the smallest possible value).

wKioL1Zo0ZvQclK0AADUgO0ZnCc808.png

 

Note : Be sure to do a full backup immediately after changing back to the full recovery model.

 

2, with no_log option

  In older versions, you can use BACKUP LOG WITH NO_LOG, or WITH TRUNCATE_ONLY. However, SQL Server 2008 has removed these options.

 

3, separate and then attach

  Detach the database and delete the log files. Then reattach the database, SQL Server will warn that the log file is missing, if you ignore this warning, the system will automatically create an empty log file. Note : Some data may be lost, see "Risks and Countermeasures of Losing Log Files"  http://jimshu.blog.51cto.com/3171847/1341289

 

http://jimshu.blog.51cto.com/3171847/932669

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326349067&siteId=291194637