join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 




Managing SQL Server 2000 Transaction Log Growth

Written By: Jeremy Kadlec -- 7/10/2006 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
We have been running SQL Server 2000 for a few years and the transaction log file has become very large for some of our databases.  In some circumstances, the transaction log is a more than 5 times larger than our database. How can I reduce the size of this file?

Solution
Most likely, the root cause for the continuous transaction log growth is related to the database recovery model being set to 'full' for your user defined databases without issuing regularly scheduled transaction log backups.  The full recovery model configuration is maintaining all of the before and after records in the transaction log until the transaction log is backed up.  Most likely, you are not backing up the transaction log on a regular basis i.e. hourly, daily, etc. to support a high availability solution such as log shipping.

With these items in mind, it is time to make some changes to your SQL Server user defined databases and get your transaction logs to a manageable size.  Let's walk through each of these steps one at a time.  Keep in mind that since you are not performing transaction log backups that this process could be completed at an as needed time, but the preference would be during a maintenance window.

Note - These commands can be run in SQL Server management tool of your choice and replace the '[YourDatabaseNameHere]' text with the needed user defined database name.
 

First, review the transaction log size prior to the shrinking process.

USE [YourDatabaseNameHere]
GO
SELECT *
FROM sysfiles
WHERE name LIKE '%LOG%'
GO
 

Second, set the database recovery model to 'simple'. 

USE [YourDatabaseNameHere]
GO
ALTER DATABASE [YourDatabaseNameHere] SET RECOVERY SIMPLE
GO
 

Third, issue a checkpoint against the database to write the records from the transaction log to the database.

USE [YourDatabaseNameHere]
GO
CHECKPOINT
GO
 

Fourth, truncate the transaction log.

USE [YourDatabaseNameHere]
GO
BACKUP LOG [YourDatabaseNameHere] WITH NO_LOG
GO
 

Fifth, record the logical file name for the transaction log to use in the next step.

USE [YourDatabaseNameHere]
GO
SELECT Name
FROM sysfiles
WHERE name LIKE '%LOG%'
GO 

Sixth, to free the unused space in your transaction log and return the space back to the operating system, shrink the transaction log file.

USE [YourDatabaseNameHere]
GO
DBCC SHRINKFILE ([FileNameFromPreviousStep], [NeededFileSize])
GO
 

Seven, review the database transaction log size to verify it has been reduced.

USE [YourDatabaseNameHere]
GO
SELECT *
FROM sysfiles
WHERE name LIKE '%LOG%'
GO

Next Steps

  • Review your key SQL Server databases to determine if the transaction log growth is out of control.
  • Review this code and modify it for one of your databases.
  • Once the scripts are modified, test the scripts in a test environment to ensure they meet your needs.
  • Schedule time to shrink your databases and communicate the configuration changes.
  • Continue to monitor the database sizes and the available disk space on your servers.
     
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Backup Pro for smaller, more robust SQL Server backups. Download a free trial now!

We fill in the gaps... SQL Server Training, Development, Performance Tuning, SSIS and more

Become a member of the MSSQLTips community

Free whitepaper - SQL Server Fragmentation Explained


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Red Gate Software - SQL Refactor

SQL Server Management Studio add-in SQL Refactor dramatically speeds up database development and administration of legacy SQL code by providing over a dozen code refactorings, including Layout SQL, Summarize Script, Encapsulate as SP, Smart Object Rename and more. Free 14-day trial download.

Download now!

More SQL Server Tools
SQL Data Generator

SQL Prompt

SQL compliance manager

SQL diagnostic manager

SQL Backup




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com