Shrinking the Log file

MS SQL stores database changes into a log file that as more activity occurs on the database the larger the log file grows. When it comes to disk space limitations and performance this becomes a considerable problem. MS SQL Server provides a command to truncate the log file

  1. DBCC SHRINKFILE(<log file>, 0, TRUNCATEONLY)

The command will shrink the log file specified. The log file name is meaningful only for a particular database. In order to run the command one needs to make sure that it is executed in the scope of the required database. To do this, MS SQL management console provides support to change the scope database through the command

  1. USE [<database name>]

Another easier way is to change the database from the drop down list provided in the SQL Editor toolbar.

A problem that arises is that one needs to determine the name of the log file. The log file name can be retrieved from the database_files table in the sys tables and finding the record with the type description set to log.

  1. DECLARE @log VARCHAR(200)
  2. SELECT @log = [name] FROM sys.database_files WHERE type_desc='LOG'

Finally, combining this information into one script that truncates a database log file:

  1. USE [<database name>]
  2.  
  3. DECLARE @log VARCHAR(200)
  4. SELECT @log = [name] FROM sys.database_files WHERE type_desc='LOG'
  5.  
  6. DBCC SHRINKFILE (@log, 0, TRUNCATEONLY)

Another problem with the log file growth is related to the way it is set to grow. By default the growth of the log file is set to increase by 10%. As the log file increases in size the more space it requires to grow and as disk space is pre-allocated to improve write performance, the large space allocation becomes a problem. The table below gives an example of the growth rate of a 100MB file during the first 10 growths.

Original SizeGrowth SizeFinal Size
1.100.00MB10.00MB110.00MB
2.110.00MB11.00MB121.00MB
3.121.00MB12.10MB133.10MB
4.133.10MB13.31MB146.41MB
5.146.41MB14.64MB161.05MB
6.161.05MB16.10MB177.15MB
7.177.15MB17.71MB194.86MB
8.194.86MB19.48MB214.34MB
9.214.34MB21.43MB235.77MB
10.235.77MB23.57MB259.34MB

From the table above it is clear that after 10 increments the log file has already exceeded 2.5times its original size. The more the file grows the bigger the increments will get.

Databases rarely need to write large portions of log file, except when a large amount of data is inserted or manipulated. To limit the growth of the log file without hindering much the performance it is best to grow the log file in well determined steps for example 10MB. To set this one can use the properties dialog in the SQL Server Management Console or alternatively through the SQL statements.

  1. ALTER DATABASE [<database name>] MODIFY FILE (NAME = N'<database log name>', FILEGROWTH = 10240KB)

Conclusion

When dealing with large growth of the log file there are two things that need to be analysed:

  1. The growth rate of the log file
  2. Regular truncation of the log file, ideally after it has been backed up through the transaction log shipping process.