Solved

Can I Shrink my SQL Server 2008 R2 Transaction files without fragmentation?

Posted on 2012-04-12
9
513 Views
Last Modified: 2012-04-16
Experts - a preemptive strike here (hopefully)

My SQL Server 2008 R2 log files are not too big yet, but they will be someday...  someday sooner than I care to admit. Our "Recovery Model" is "Bulk Logged" (instead of "Simple" or "FULL").

Will his option work on a "Bulk Logged"  DB?

Please First Read this post before you give me your best answer... it was a painful revelation to me (who is a big fan of the truncate) .. shrinks everything so well... but fragments your DB almost as much as possible.

I need a better way... any ideas!!
0
Comment
Question by:dantheanswerman
9 Comments
 
LVL 5

Expert Comment

by:hafeezmca
Comment Utility
Hi,

Sometime, it looks impossible to shrink the Truncated Log file. Following code always shrinks the Truncated Log File to minimum size possible.
 
USE DatabaseName
 GO
 DBCC SHRINKFILE(<TransactionLogName>, 1)
 BACKUP LOG <DatabaseName> WITH TRUNCATE_ONLY
 DBCC SHRINKFILE(<TransactionLogName>, 1)
 GO

Read this article as well.

http://blog.sqlauthority.com/2010/05/03/sql-server-shrinkfile-and-truncate-log-file-in-sql-server-2008/
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
hafeezmca,

Sometime, it looks impossible to shrink the Truncated Log file. Following code always shrinks the Truncated Log File to minimum size possible.
There are two problems with this:
1.  It is a very bad idea.
2. The author is using SQL Server 2008R2 and thankfully TRUNCATE_ONLY is no longer supported.  If you had actually read the article you posted you would have seen that.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
Comment Utility
My SQL Server 2008 R2 log files are not too big yet, but they will be someday
You need to size your Transaction Log so that there is no need to shrink it.  It does not get more complicated than that.

You should only shrink your data files if it is an emergency.  Is that what you are asking?  If the answer is yes, than it is quite simple:
1.  Find a window when there is little or no activity and back up the Transaction Log.
2.  Shrink the Transaction Log to the appropriate size.
0
 
LVL 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 250 total points
Comment Utility
In your question you specifically say Transaction Log files.  Truncating Transaction log files do not fragment your database.  Only shrinking your Data files will fragment your database.  Log files are circular and you should only have one, so shrinking your t-log file will only release space back to the last open transaction, but it is not going to fragment your database.
As was mentioned, you should size your t-log file so that you are not growing it or shrinking it.   The most important thing about the t-log file is that you have less Virtual Log Files (DBCC LOGINFO) and that the file be as contiguous on the disk as possible because it is circular and benefits from sequential reads and writes.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Truncating Transaction log files do not fragment your database.
Absolutely.  However truncating the Transaction Log file has the potential to be far more dangerous, in that you have rendered your restore plan based on the Transaction Log useless.
0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
Comment Utility
acperkins:
Do you mean if you run BACKUP LOG WITH TRUNCATE_ONLY?  Because if you use DBCC SHRINKFILE on the T-Log you are not rendering the restore plan useless as it only releases the unused space.  But you are right, TRUNCATE_ONLY will ruin the last log recovery for restores.  But TRUNCATE_ONLY is deprecated and does not exist in 2005 or 2008 +.

I hope I read your answer right.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Do you mean if you run BACKUP LOG WITH TRUNCATE_ONLY?
Yes.  You break the backup chain, so that unless you do an immediate Full backup you will not be able to do a point-in-time restore.

Because if you use DBCC SHRINKFILE on the T-Log you are not rendering the restore plan useless as it only releases the unused space.
Correct.

But TRUNCATE_ONLY is deprecated and does not exist in 2005 or 2008 +.
Actually it did exist in SQL Server 2005 and could be used, although it was deprecated. SQL Server 2005 BOL had the following note:
The BACKUP LOG WITH NO_LOG and WITH TRUNCATE_ONLY options have been discontinued. If you are using the full or bulk-logged recovery model recovery and you must remove the log backup chain from a database, switch to the simple recovery model. For more information, see View or Change the Recovery Model of a Database (SQL Server).

It is no longer supported as of SQL Server 2008.  Thankfully.
0
 
LVL 6

Expert Comment

by:SJCFL-Admin
Comment Utility
Can I reframe the question?

What do I need to look at to keep my log files from getting out of hand?

Applications that are not taking frequent enough commits.
Back to back index rebuilds without intervening transaction log backups
    (and I prefer a method that only rebuilds indexes exceeding a certain level of fragmentation. Run it more frequently to rebuild fewer indexes at a time and space out the transaction log activity.)
Replication activity with long lag times. (leaving transaction log records non-deletable)

Keep an eye on the above and the logs take better care of themselves...
0
 
LVL 2

Author Closing Comment

by:dantheanswerman
Comment Utility
Thanks both of you for your excellent, informative comments... I am really taking a good hard look at these DB's and determining the best plan. Knowing a little more, my real question (which I will ask in a separate question - and give credit you guys for your hard work) ..

New question -

What is a good plan for DR (Disaster Recovery) for a MAS_500 7.4 DB?

I want to keep the transaction log and data files a reasonable size and be able to recover if needed. We are using the bulk-logged method (which I inherited).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now