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.

55 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)

Unknown 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

Unknown 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)

Unknown 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)

Unknown said...
This comment has been removed by the author.
Business Analytics Course said...

It's a smart blog. I mean it seriously. You have so much knowledge on this subject and so much passion. He also knows how to get people to join him, obviously from the answers.

360DigiTMG Business Analytics Course in Bangalore

Data Analytics Course said...

I really appreciate this wonderful message you have given us. I assure you that would be beneficial for most people.

360DigiTMG Data Analytics Course in Bangalore

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

Data Science Training said...

I truly like only reading every one your web logs. Simply desired to in form you which you simply have persons such as me that love your own work out. Absolutely an extraordinary informative article. Hats off to you! The details which you have furnished is quite valuable. Tableau Course in Bangalore

Data Science Institute In Banglore said...


Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.

Data Science Training Institute in Bangalore

Best Data Science Courses In Bangalore said...


Additionally, this is an excellent article which I truly like studying. It's not everyday I have the option to see something similar to this.
Data Science Course In Bangalore With Placement

Cyber Security Course In Bangalore said...


Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .

Cyber Security Course In Bangalore

Mike Johnson said...

Maybe you can make short video about it for tiktok? On this site https://soclikes.com/buy-tiktok-likes you can get tiktok likes

Data Science Courses In Bangalore said...


I'm really thankful that I read this. It's extremely valuable and quite informative and I truly learned a great deal from it.

Data Science Course In India

Data Science Course Syllabus said...


Nice to be seeing your site once again, it's been weeks for me. This article which ive been waited for so long. I need this guide to complete my mission inside the school, and it's same issue together along with your essay. Thanks, pleasant share.

Data Science Course Syllabus

DataScience Specialist said...

I have to search sites with relevant information ,This is a
wonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Course in Bangalore

Datascience Course Analyst said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
Data Science Course in Bangalore

Datascience Books said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
Data Science Training in Bangalore

InstituteBlr said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
data analytics course in bangalore

AI course in pune said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
artificial intelligence course in pune

data analytics books said...

I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
data analytics training in bangalore

Srigokul said...

Useful information, Thank you for sharing...

Data science training in chennai
Data science course in chennai

Huongkv said...

Đặt vé máy bay tại Aivivu, tham khảo

mua ve may bay di my

đăng ký bay từ mỹ về việt nam

vé máy bay đà nẵng nha trang

vé máy bay nha trang phú quốc

giá vé máy bay đi Huế

Mallela said...

Thanks for posting the best information and the blog is very helpful.data science interview questions and answers

Digital Marketing training - 360DigiTMG said...

Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.
Data Science Training in Raipur

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore

Data Science in Pune said...

I found Habit to be a transparent site, a social hub that is a conglomerate of buyers and sellers willing to offer digital advice online at a decent cost.
Data Science Course in Pune

Datascience Books said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
Data Science Training in Bangalore

data analytics books said...

I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
data analytics training in bangalore

InstituteBlr said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
data analytics course in bangalore

Unknown said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavours.
data science course in bangalore with placement

Datascience Course Analyst said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
Data Science Course in Bangalore

Data Analytics Courses in Bangalore said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data science course fees in bangalore

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
data scientist course in bangalore

Datascience Course Analyst said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
Data Science Course in Bangalore

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore

Mallela said...

Thanks for posting the best information and the blog is very helpful.data science institutes in hyderabad

Data Science said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Training in Chennai

Best Data Science Courses in Bangalore said...

I enjoyed the coursework, the presentations, the classmates and the teachers. And because my company reimbursed 100% of the tuition, the only cost I had to pay on my own was for books and supplies. Otherwise, I received a free master's degree. All I had to invest was my time.

Best Data Science Courses in Bangalore

Data Analytics Course in Bangalore said...

I like to see websites that include the price of free shipping from the excellent helpful resource. I really enjoyed reading your post. Thank you!

Data Analytics Course in Bangalore

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
data scientist course in bangalore

Digital Marketing Course in Bangalore said...

Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

Digital Marketing Course in Bangalore

InstituteBlr said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
data analytics course in bangalore

princika said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data science course

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore

princika said...

I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data science course in chennai

Data Science said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Course Syllabus

data analytics books said...

I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
data analytics training in bangalore

Mallela said...

Thanks for posting the best information and the blog is very important.artificial intelligence course in hyderabad

Deekshitha said...

Informative blog
ai training in hyderabad