Thursday, March 4, 2010

My SQL LDF file is so big in comparison with the MDF file, what can I do ?

At a lot of customer sites I  have seen situations in which the transaction log file (LDF) is multiple times bigger than the MDF file. This is not a 'normal' situation. In this blog I will explain what you can do if you have an LDF file of your SQL server database which is too big in comparison with the size of your MDF file.



What is the difference between the MDF and LDF file ?
The .MDF file is the primary data file of a SQL database. The .LDF file is the transaction log file of a SQL database. The transaction log is used to guarantee the data integrity of the database and for data recovery. Data integrity is implemented by design and can't be configured by the database administrator. Data recovery is implemented by design and is configurable by the database administrator.

In SQL Server you have 3 different data recovery models: Simple, Full and bulk-logged. Typically, a database uses the Full recovery model or Simple recovery model.

What is the main difference between Full and Simple?
  • Simple: No transaction log backups. Changes since the most recent backup are unprotected. In the event of a disaster, those changes are lost and need to be redone. You can only recover to the end of a backup.
  • Full: Requiers transaction log backups. No work is lost due to a lost or damaged data file. Can recover to a specific point in time, assuming that your backups are complete up to that point in time. For example, prior to application or user error. 

What is in the transaction log in the Simple recovery model ?
All open transactions. This means not committed transactions. This is called the active part of the transaction log. At the moment a transaction is committed it will be truncated from the transaction log. Log truncation automatically frees disk space for reuse by the transaction log. The biggest size of the transaction log will be the size of all open transactions at one moment. Therefor in a 'normal' SQL production database the transaction log file will never get very big.

What is in the transaction log in the Full recovery model ?
All committed transaction (inactive part) and open transactions (active part). The log records cannot be truncated untill all its log records have been captured in a transaction log backup. The log is truncated when you backup the transaction log.

What can we do to lower the size of the transactional log file (LDF)?
It depends on the configured recovery model of your database, what you can do. The recovery model of your database can be found in the properties of the database:

or via SSMS:
Open a query window and select the database.

USE
SELECT Recovery_model_desc FROM sys.databases WHERE Name = db_name()


What to do if your recovery model is: FULL?
Check if you are making transactional backups on a reguler time interval. If you only make full backups your transaction log will always grow and will never be truncated. In case you never want to lose data in case of a application or user error. Start making transactional log backups. If a database restore to the latest full backup is acceptable, you can switch the recovery model of your database to simple. Before switching the recovery model of your database please read next document: Considerations from switching the recovery model of your database. After switching to the recovery model Simple , you can shrink the log file. See the end of this blog how to do this.


What to do if your recovery model is: Simple?
Shrink the log file.


To set the recovery model to Simple you can run next query:
ALTER DATABASE
SET RECOVERY SIMPLE

To set the recovery model to FULL you can run next query:
ALTER DATABASE
SET RECOVERY FULL

How many free space is available in my log file?
  • Select the database in the Object Explorer
  • Point to Tasks, Point to Shrink and then click Files.
  • Select File Type: Log
  • Currently allocated: The current size of your log file
  • Available free space: The free space in your log file
To shrink the log file:
  • Select the database in the Object Explorer
  • Point to Tasks, Point to Shrink and then click Files.
  • Select File Type: Log
  • Select Release unused space
  • Press OK to start the shrink process. If you do not want to shrink the log file press the cancel button.

9 comments:

Alexis said...

I seldom work with sql server and files too. But once I couldn't use my files,because of they were corrupted. And fortunately I incidentally entered in the Internet and observed there - sql server repair database. The utility resolved my issue for a minute and free of charge as I bore in mind.

Raj said...

Hello Dear,

i had many confusion about mdf, ldf ndf files of sql server database. after reading your article, i am mostly satisfy.

I have also written article about database recovery software.

נמרוד כנען said...

Nice articcal

Andrei said...

This article helped me in shrinking the log file size. Very nice indeed. Helped me a lot... I recovered 7Gigs of space. Thanks!

All mankind love a lover. said...

i have a database my mdf fils is 139 mb & ldf file is 29.7 gb I've taken transaction log backup and then shrink database file it is reduced but only 1 gb what should i do to reduce the size of ldf file (gmailbackup@sify.com)

All mankind love a lover. said...

i have a database my mdf fils is 139 mb & ldf file is 29.7 gb I've taken transaction log backup and then shrink database file it is reduced but only 1 gb what should i do to reduce the size of ldf file (gmailbackup@sify.com)

Adam Gorge said...

HI André,

I also wrote an article on this topic. I cover all the possible causes of growing transaction log file as well as possible solutions. Please have a look on this article: Decrease Transaction Log File Size

Krunal Patel said...

All mankind love a lover,
Alter your database to simple recovery model and then shrink LDF to 1.

ALTER DATABASE SET RECOVERY SIMPLE

DBCC SHRINKFILE(,1)

Krunal Patel said...

All mankind love a lover,
Alter your database to simple recovery model and then shrink LDF to 1.

ALTER DATABASE "DBNAME" SET RECOVERY SIMPLE

DBCC SHRINKFILE("Logical Name of your Logfile",1)