Published on

December 24, 2023

How to Reduce the Size of SQL Server Transaction Log Files

If you have been running SQL Server for a while, you may have encountered a situation where the transaction log file has become very large compared to your database size. This can cause issues with disk space and performance. In this article, we will discuss how to reduce the size of the transaction log file in SQL Server.

Identifying the Root Cause

The first step in resolving this issue is to identify the root cause of the continuous transaction log growth. In most cases, this is related to the database recovery model being set to ‘full’ without regularly scheduled transaction log backups. The full recovery model stores all before and after records in the transaction log until it is backed up.

If you are not performing regular transaction log backups, it is time to make some changes to your SQL Server user-defined databases and get your transaction logs to a manageable size.

Step-by-Step Solution

Follow these steps to reduce the size of your SQL Server transaction log files:

  1. Review the transaction log size prior to the shrinking process:
  2.     
        USE [YourDatabaseNameHere]
        GO
        SELECT * FROM sysfiles WHERE name LIKE '%LOG%'
        GO
        
      
  3. Set the database recovery model to ‘simple’:
  4.     
        USE [YourDatabaseNameHere]
        GO
        ALTER DATABASE [YourDatabaseNameHere] SET RECOVERY SIMPLE
        GO
        
      
  5. Issue a checkpoint against the database to write the records from the transaction log to the database:
  6.     
        USE [YourDatabaseNameHere]
        GO
        CHECKPOINT
        GO
        
      
  7. Truncate the transaction log:
  8.     
        USE [YourDatabaseNameHere]
        GO
        BACKUP LOG [YourDatabaseNameHere] WITH NO_LOG
        GO
        
      
  9. Record the logical file name for the transaction log to use in the next step:
  10.     
        USE [YourDatabaseNameHere]
        GO
        SELECT Name FROM sysfiles WHERE name LIKE '%LOG%'
        GO
        
      
  11. To free the unused space in your transaction log and return the space back to the operating system, shrink the transaction log file:
  12.     
        USE [YourDatabaseNameHere]
        GO
        DBCC SHRINKFILE ([FileNameFromPreviousStep], [NeededFileSize])
        GO
        
      
  13. Review the database transaction log size to verify it has been reduced:
  14.     
        USE [YourDatabaseNameHere]
        GO
        SELECT * FROM sysfiles WHERE name LIKE '%LOG%'
        GO
        
      

By following these steps, you can effectively reduce the size of your SQL Server transaction log files and optimize the performance of your database.

Remember to regularly schedule transaction log backups to prevent the transaction log from growing excessively in the future.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.