<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>StraightPath Consulting&#039;s SQL Server Blog &#187; Shrinking &amp; Transactions</title>
	<atom:link href="http://www.straightpathsql.com/archives/category/shrinking-transactions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.straightpathsql.com</link>
	<description>Mike Walsh&#039;s Thoughts on SQL Server, Professional Development and Life</description>
	<lastBuildDate>Thu, 29 Jul 2010 12:11:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Manage Your Transactions (So They Don&#8217;t Manage You &#8211; Part 2)</title>
		<link>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-2/</link>
		<comments>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-2/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 01:43:00 +0000</pubDate>
		<dc:creator>Mike Walsh</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pet Peeve]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Shrinking & Transactions]]></category>
		<category><![CDATA[Syndicate]]></category>
		<category><![CDATA[Pet Peeves]]></category>

		<guid isPermaLink="false">http://straightpathsql.mikewalshonline.com/?p=36</guid>
		<description><![CDATA[

This is the final in a series on shrinking and transactions. You can see them all&#160;in the Shrinking &#38; Transactions category.
In part 1, we talked about factors that affect transaction log growth. We talked about what a SQL Server transaction is and the various modes SQL interacts with a transaction (Autocommit, Implicit Transactions and Explicit [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.straightpathsql.com%252Farchives%252F2009%252F04%252Fmanage-your-transactions-so-they-dont-manage-you-part-2%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Manage%20Your%20Transactions%20%28So%20They%20Don%27t%20Manage%20You%20-%20Part%202%29%20%23%22%20%7D);"></div>
<p><em>This is the final in a series on shrinking and transactions. You can see them all&nbsp;in the <a href="http://www.straightpathsql.com/blog/category/shrinking-transactions">Shrinking &amp; Transactions</a> category.</em></p>
<p>In <a href="http://www.straightpathsql.com/blog/2009/4/9/manage-your-transactions-so-they-dont-manage-you-part-1.html">part 1</a>, we talked about factors that affect transaction log growth. We talked about what a SQL Server transaction is and the various modes SQL interacts with a transaction (Autocommit, Implicit Transactions and Explicit Transactions).</p>
<p>The theme of that post was about managing your transactions so they don&#8217;t manage you. In that same theme, let&#8217;s continue being in charge of our transactions. Rather than from a physical size perspective, let&#8217;s look at it from a performance standpoint.</p>
<p><strong>Database Engine/Administration Factors</strong></p>
<p>When it comes to ensuring that transaction logging and transactions don&#8217;t cause performance bottlenecks there are a few things DBAs should remember and look at, I&#8217;ll highlight a few of them.</p>
<p><strong>Pay careful attention to the I/O of your transaction log &#8211; </strong>As I discussed in <a href="http://www.straightpathsql.com/blog/2009/4/9/manage-your-transactions-so-they-dont-manage-you-part-1.html">Part 1</a>, the transaction log record needs to be written first and this is vital to remember. Keep your Transaction log on fast I/O, separate I/O and follow best practices with your log file.</p>
<p>A great <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/8-Steps-to-better-Transaction-Log-throughput.aspx">blog post</a> by Kimberly Tripp with eight tips here goes through many more details that I have often hinted about in various posts. Some of her tips are best practices for the data file as well.</p>
<blockquote>
<p>She mentions things like: <strong>Pre-Allocate Your Space!!</strong> This is a huge pet peeve of mine. Plan for your capacity and start out with the size you think you will need. This will stop costly <strong>autogrowths </strong>which can lead to <strong>fragmentation </strong>and<strong> waits (for the growth.) </strong></p>
<p>Or <strong>Defragment the physical volume </strong>, people spend a lot of time in the database world looking at index fragmentation (<strong>at least I <em>hope </em>you do!) </strong>What about your physical storage?</p>
</blockquote>
<p>When it comes to choosing your RAID, remember the transaction log is going to want <strong>fast writes. </strong>These writes are <strong>sequential. </strong>These writes are <strong>writes. </strong>RAID 5 (<a href="http://en.wikipedia.org/wiki/Standard_RAID_levels">Wikipedia article on RAID</a>) may not generally be a <em>horrible </em>solution for your data (.MDF/.NDF) files. For the Log files, it just doesn&#8217;t make sense. RAID 5 incurs extra cost for each write (essentially 2 reads and 2 writes for each write because of the maintenance of parity for fault tolerance).</p>
<p>If cost is an issue, look at just mirroring. This will give you some fault tolerance (<strong>Remember, the transaction log is key to a happy, consistent database as we talked about in Part 1</strong>) and still gives you the same performance as writing to one disk.</p>
<p>If the mirror doesn&#8217;t give you great performance for your high end, high throughput applications, consider RAID 10 or RAID 0+1 (both are essentially combining <strong>striping and mirroring </strong>in various means). These will give you the redundancy with the performance benefit of a stripe.</p>
<p><strong>Don&#8217;t get into a shrink/grow. shrink/grow pattern. </strong>I have harped on this one. Set it to the size you think you need, manage it&#8217;s growth and monitor it. Tibor Karaszi has an excellent post about log shrinking and it&#8217;s effect on <a href="http://www.karaszi.com/SQLServer/info_dont_shrink.asp"><strong>Virtual Log Files (VLFs)</strong></a>.</p>
<p><strong>Pick the right recovery model! </strong>As I discussed in Part 1, recovery models are important for log growth. It&#8217;s also important to pick the right model for your recovery needs (in fact much more important to pick for <strong>recovery </strong>needs.. (<a href="http://www.straightpathsql.com/blog/2009/1/14/do-you-focus-too-much-on-your-backups.html">Remember we do backups for <strong>restores</strong> in fact it&#8217;s the restores that are more important</a>). Let&#8217;s presume that you care about point in time recovery and you pick <strong>Full Recovery</strong>:</p>
<p>That&#8217;s great. Now get a strategy to <strong>PERFORM LOG BACKUPS. </strong>Being in full recovery mode without doing full backups is pointless. This tells me you aren&#8217;t performing restore drills (<strong>are you?</strong>), you are probably <em><strong>truncating and shrinking </strong></em>your log file when you run out of space and you potentially can&#8217;t meet<strong> SLAs or expectations</strong>. This will cause all of the above issues but it will also manifest itself in performance problems. So get with the program and do those log backups on a schedule that meets your required recoverability (<a href="http://www.straightpathsql.com/blog/2009/3/5/where-do-i-start.html"><strong>one of the &#8220;itties&#8221; we should all care about</strong></a>) and manages growth/performance best.</p>
<p>These are some of the highlights that quickly come to mind that a DBA can have effect on. There are definitely more, check out Kimberly&#8217;s post above and look around her blog, great stuff to keep in mind.</p>
<p>What about:</p>
<p><strong>Things Developers Can Be Mindful Of</strong></p>
<p><strong>KISS &#8211; Keep it short stupid</strong> &#8211; Alright, the stupid part was uncalled for but it worked. The key to remember when developing is if you are holding a transaction open, you are holding <strong>locks </strong>for the duration of your transaction.</p>
<p>What kind of locks you are holding depends on your operation (Select, DML, DDL, etc) and your <a href="http://msdn.microsoft.com/en-us/library/ms173763.aspx">transaction isolation level</a> and any <a href="http://technet.microsoft.com/en-us/library/ms187373.aspx">lock hints</a> you may be using (Andrew Kelly has a great <a href="http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx">post </a>about the dangers of <strong>NOLOCK or SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED</strong>). Remember that when you are in a transaction, whatever lock you had to hold to perform your operation is <strong>held until that transaction is committed.</strong></p>
<p>If you perform an update within an explicit transaction (<strong>BEGIN TRANSACTION</strong>) and you are in the default isolation level of SQL Server (Read Committed) you will be holding some sort of exclusive lock <strong>even after your update statement is finished</strong>. SQL doesn&#8217;t know if you are going to rollback or commit your work so it has to lock either the rows, page or table (depending on the grain of lock chosen) <strong>until you tell SQL that you are done</strong>. This means that you can <strong>block other updates, other reads or other operations until you have committed/rolled back your transaction. </strong></p>
<p>This is excellent behavior out of a DBMS. You want to hold a lock until you are done. Otherwise you might as well just throw data integrity out the window. It&#8217;s <strong>bad</strong> when you hold that poor transaction hostage longer than need be. So, the solution is simple:</p>
<ul>
<li><strong>Keep your transactions as short as possible</strong></li>
<li><strong>Properly handle errors and rollback when necessary</strong></li>
<li><strong>COMMIT as soon as possible</strong></li>
<li><strong>Don&#8217;t try to solve world peace and hunger within one transaction </strong>- I cringe when I see a single &#8220;BEGIN TRANSACTION&#8221; and then multiple page downs of operations, <strong>cursors</strong>, select statements, crazy logic, etc before you get to the commit or rollback. This means that all of those locks are held and you are probably causing blocking until you are done. <strong>Batch out work.</strong></li>
<li><strong>Even with implicit transactions, don&#8217;t try to do too much work in one operation. </strong>If you are updating an entire table, and it is a huge table, try and batch it out, allow the work to commit, allow a checkpoint to fire and keep your log from growing.</li>
</ul>
<p><strong>You tell those Writers, we just want to read!</strong></p>
<p>Sorry, you hold locks also. <strong>When you issue a select you are still holding a potentially blocking lock</strong>. This depends on isolation level and lock hints but in the default isolation level you are holding a lock. This is to prevent you from reading data that is about to be rolled back. <strong>Readers Can (and often do) block writers</strong>. If you are in a busy OLTP system and experience a lot of timeouts on update/insert operations look at your reporting queries, your select queries.</p>
<p><strong>Optimize your code for performance. </strong>This holds true for updates/inserts and selects but watch your selects, if it takes you 10 seconds to bring back data to a screen, that is 10 seconds (per select) that you are potentially blocking a writer. Even if it takes 20ms but is executed quite frequently, that is a potentially substantial situation. <strong>Look at the performance of every piece of code you release!</strong> Query tuning doesn&#8217;t have to be difficult. One place to start is this <a href="http://www.straightpathsql.com/blog/2009/2/20/just-add-a-join-hint.html">post </a>but a quick search will yield a lot of tips. Check out my <a href="http://www.straightpathsql.com/blogroll/">blogroll</a>, a lot of excellent advice on those blogs.</p>
<p>Think about performance at the beginning of a project rather than the end and your life will be easier in production. <strong>DBAs &#8211; </strong>make performance be an initiative at the beginning. Yes you are too busy for proactive work right now, <strong>make time for it anyway. </strong>When you pay later you always pay a lot more.</p>
<p>Again, with Part 1, I am hoping that a lot of this was review and simple. At the same time, if it helped anyone, great. There is plenty of advice out there and some careful thought and consideration to managing your transactions and .LDFs will go a long way.</p>
<p>&nbsp;</p>
<p><span style="text-decoration: underline;"><strong>Related Posts At StraightPath SQL</strong></span></p>
<ul>
<li><a href="http://www.straightpathsql.com/blog/2009/4/9/manage-your-transactions-so-they-dont-manage-you-part-1.html">Manage Your Transactions (So They Don&#8217;t Manage You &#8211; Part 1)</a></li>
<li><a href="http://www.straightpathsql.com/blog/2009/1/6/dont-touch-that-shrink-button.html">Don&#8217;t Touch That Shrink Button!</a></li>
<li><a href="http://www.straightpathsql.com/blog/2009/1/31/shrinking-is-a-popular-topic.html">Shrinking Is A Popular Topic</a><span style="text-decoration: underline;"><br /></span></li>
</ul>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.straightpathsql.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage Your Transactions (So they don&#8217;t manage you &#8211; Part 1)</title>
		<link>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-1/</link>
		<comments>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-1/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 13:19:02 +0000</pubDate>
		<dc:creator>Mike Walsh</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pet Peeve]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Shrinking & Transactions]]></category>
		<category><![CDATA[Syndicate]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pet Peeves]]></category>

		<guid isPermaLink="false">http://straightpathsql.mikewalshonline.com/?p=34</guid>
		<description><![CDATA[

This is part 3 in a series on shrinking and transaction management. Click here for part 4. You can see them all in the Shrinking &#38; Transactions category.
If you are one of the people who have been reading this blog, you know where I stand on shrinking databases/log files. I don&#8217;t like&#160;the practice&#160;(Post 1, Post [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.straightpathsql.com%252Farchives%252F2009%252F04%252Fmanage-your-transactions-so-they-dont-manage-you-part-1%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Manage%20Your%20Transactions%20%28So%20they%20don%27t%20manage%20you%20-%20Part%201%29%20%23%22%20%7D);"></div>
<p><em>This is part 3 in a series on shrinking and transaction management. Click <a href="http://www.straightpathsql.com/blog/2009/4/15/manage-your-transactions-so-they-dont-manage-you-part-2.html">here</a> for part 4. You can see them all in the <a href="http://www.straightpathsql.com/blog/category/shrinking-transactions">Shrinking &amp; Transactions</a> category.</em></p>
<p>If you are one of the people who have been reading this blog, you know where I stand on shrinking databases/log files. I don&#8217;t like&nbsp;the practice&nbsp;(<a href="http://www.straightpathsql.com/blog/2009/1/6/dont-touch-that-shrink-button.html">Post 1,</a> <a href="http://www.straightpathsql.com/blog/2009/1/31/shrinking-is-a-popular-topic.html">Post 2</a>, SQLServerPedia <a href="http://sqlserverpedia.com/wiki/Shrinking_Databases">article</a>.)</p>
<p>That being said, let&#8217;s look at a couple of reasons why transaction logs can grow and what you can do about that in a proactive mode. We&#8217;ll break this down into a couple posts.</p>
<p>Through answering forum questions and some recent conversation at a Microsoft SQL Tuning class, it strikes me that there are some misconceptions about how transactions work, when one needs to commit/rollback and what the recovery models actually do to your database. Hopefully we can rake through some of that today.</p>
<p><strong><span style="font-size: 110%;">What is a transaction?</span></strong></p>
<p>This is probably a review, so let&#8217;s just hit the quick points:</p>
<ul>
<li>An atomic unit of work that either succeeds or fails together as one.</li>
<li>Classic example is the credit/debit example (Note this doesn&#8217;t work with government budgets, think of your checking account)
<ul>
<li>You are online with your bank and initiate a transfer from your checking account to your loan account. This transaction includes a withdrawal and deposit. What happens if there is a failure between operations? Depends on if the work was done in a transaction:</li>
<li>In a Transaction &#8211; The withdrawal that happened is undone (rollback) and everything is fine.</li>
<li>No Transaction &#8211; The withdrawal completes (commit) and the deposit never happens. You are out $300 and have it sitting in limbo land someplace.</li>
</ul>
</li>
</ul>
<p><strong>Implicit/Explicit/Autocommit?</strong></p>
<p>There are three modes that SQL Server can operate in with regards to transactions. Implicit and Explicit transactions or in autocommit mode. Let&#8217;s explore those:</p>
<ul>
<li><strong>Autocommit Mode &#8211; </strong>This is default behavior in SQL Server. This is also where a lot of people can get tripped up in their thinking (more on that in a minute&#8230;). Basically here, if you were to issue a SQL query that modifies data, once that statement completes (without error) the work you did is automatically committed.</li>
<li><strong>Explicit Transaction &#8211; </strong>This is also default behavior in SQL Server. If you want to declare a transaction yourself and specify when to commit or rollback a unit of work you use the <strong>BEGIN TRANSACTION, ROLLBACK TRANSACTION </strong>or <strong>COMMIT TRANSACTION </strong>commands. You declare your transaction and then through proper error handling manage when to rollback or&nbsp; commit based on your business rules. Through this method you can have multiple statements inside the same transaction as opposed to what happens with autocommit.</li>
<li><strong>Implicit Transaction Mode &#8211; </strong>This is off by default but you can enable it within a session with this command: SET IMPLICIT_TRANSACTIONS ON. You can read more about it in books online (<a href="http://msdn.microsoft.com/en-us/library/ms188317.aspx">here</a>). Basically anytime you issue a command that can modify data, SQL Server will implicitly issue a behind the scenes BEGIN TRANSACTION for you. Every statement issued from that same connection is part of that one transaction and you have to manually issue a rollback/commit command to complete that transaction. Forget to do it and now you have one big transaction which has it&#8217;s drawbacks (see below).</li>
</ul>
<p><strong><span style="font-size: 110%;">My Log is still growing! I didn&#8217;t do a BEGIN TRAN?!</span></strong></p>
<p>So you are doing ETL, doing a large delete operation or otherwise modifying a lot of data at once. You are in simple recovery mode (transaction log is overwritten at certain periods, more on recovery models <a href="http://sqlserverpedia.com/wiki/Recovery_Models">here</a>) because you don&#8217;t need to restore to a point in time. Since you haven&#8217;t declared a transaction, why on earth is SQL Server&#8217;s transaction log growing out of control?</p>
<p><strong>The answer is simple: <em>because it cares</em></strong>. It doesn&#8217;t matter which recovery model you have chosen, SQL Server will <span style="text-decoration: underline;">always</span>&nbsp;write to the transaction log. In order to maintain consistency in the database, before something is considered as having been completed it needs to be in the transaction log. It will eventually write to the physical disk but it has to be in the transaction log before your transaction is complete (see why it&#8217;s so important to put your transaction log on a separate drive and great IO??). If something were being written to the physical .mdf file but not complete when someone pulls the power plug from your server, we have a reasonable expectation of SQL to come back up in a consistent state. This is called recovery. Any transactions that are marked as committed in the log file are essentially rolled forward to the data files. Any that were not done but started are rolled back.</p>
<p>The recovery models just tell SQL how some operations get logged and when that log file is marked for reuse. In FULL recovery mode, it is not reusable <em>until</em> you do your log backup. In Simple recovery mode it is reusable once the data is written to the physical disk and no longer needed in the log, essentially.</p>
<p>The consistency and recovery means something to us when combined with the default mode that SQL operates in:</p>
<ul>
<li><strong>Every statement is a transaction whether you tell SQL it is or isn&#8217;t. </strong>This is a great thing for us and our data quality.</li>
</ul>
<p>So if you think about that statement, it makes sense that one big update or delete statement would cause your log file to grow &#8220;out of control&#8221; (it&#8217;s actually very much in control, just not your expected results <img src='http://www.straightpathsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<p>Try&nbsp;and think about different methods for handling what you are doing:</p>
<ul>
<li>Can you batch your work into smaller units? Issuing several updates,inserts or deletes dealing with a smaller amount of rows each time can help manage log growth. (<em>The log will only be reused only to the point of the longest running transaction, if you have one big query it&#8217;s all one transaction).</em></li>
<li>When deleting, are you deleting most of the rows? Maybe consider doing a truncate and recreate/repopulate of the table if the rows remaining after the delete will be less than those deleted.</li>
<li>Consider changing recovery models &#8211; Going to Simple or Bulk Logged&nbsp;recovery mode won&#8217;t help you if it is all one being done in one operation. It will potentially help the reuse if you batch it out into manageable chunks. <em>If you change your recovery mode, look it up on books online, you will most likely have to perform a full backup to keep your log chain intact and maintain restorability!</em></li>
<li>Staying in Full Recovery mode? Perform frequent log backups. Again this won&#8217;t help you with the growth of your transaction log during one large operation but it does help with multiple batches.</li>
<li>Look at using SSIS for ETL work, various options within SSIS can help manage this better and do your loads in a more efficient manner.</li>
</ul>
<p><strong>While I have your attention &#8211; </strong></p>
<p>I mentioned above one good reason to have your transaction log separate. This falls into the &#8220;widespread news&#8221; category but it&#8217;s my experience it isn&#8217;t so widely followed. Separate your transaction logs onto a separate drive when you can! Log write performance is incredibly important. If you are going to do RAID at all on it, do RAID 10 or RAID&nbsp;1 as performance is important&nbsp;(hence no RAID 5) and redundancy is important (hence no RAID 0- striping only).</p>
<p>While we are at it do you put your log backups (are you taking any???) on a separate drive also? If not, what happens if your log file drive crashes and you want to restore a log file backup? Oops.. Separate (Physically, not partitions on the same drive) your usage of drives. Yes budgets come into play but there are some basics you should try and follow where possible&#8230;</p>
<p>&nbsp;</p>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.straightpathsql.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.straightpathsql.com/archives/2009/04/manage-your-transactions-so-they-dont-manage-you-part-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Shrinking is a popular topic&#8230;</title>
		<link>http://www.straightpathsql.com/archives/2009/01/shrinking-is-a-popular-topic/</link>
		<comments>http://www.straightpathsql.com/archives/2009/01/shrinking-is-a-popular-topic/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 20:47:38 +0000</pubDate>
		<dc:creator>Mike Walsh</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Shrinking & Transactions]]></category>
		<category><![CDATA[Syndicate]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pet Peeves]]></category>

		<guid isPermaLink="false">http://straightpathsql.mikewalshonline.com/?p=23</guid>
		<description><![CDATA[

This is part 2 in a series on Shrinking &#8220;Best Practices&#8221; and logging. Click here for part 3. You can see them all in the Shrinking &#38; Transactions category.
A while back I posted about not touching the shrink button. I am still finding it fun to see what search queries bring traffic to my blog [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.straightpathsql.com%252Farchives%252F2009%252F01%252Fshrinking-is-a-popular-topic%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Shrinking%20is%20a%20popular%20topic...%20%23%22%20%7D);"></div>
<p><em>This is part 2 in a series on Shrinking &#8220;Best Practices&#8221; and logging. Click <a href="http://www.straightpathsql.com/blog/2009/4/9/manage-your-transactions-so-they-dont-manage-you-part-1.html">here</a> for part 3.</em> <em>You can see them all in the </em><a href="http://www.straightpathsql.com/blog/category/shrinking-transactions"><em>Shrinking &amp; Transactions</em></a><em> category.</em></p>
<p>A while back I posted about <a href="http://www.straightpathsql.com/blog/2009/1/6/dont-touch-that-shrink-button.html">not touching the shrink button</a>. I am still finding it fun to see what search queries bring traffic to my blog so I look at the search query log every once in awhile. That shrink post is coming up the most in searches which leads me to believe I should add a few more points.</p>
<p>As I said in that post there are some great comments on shrinking over at Paul Randal&#8217;s <a href="http://www.sqlskills.com/BLOGS/PAUL/">blog</a>. I suggest you definitely take a visit there, in fact if you had to stop reading and go to his blog, I wouldn&#8217;t be offended in the least.</p>
<p>Let&#8217;s talk about some of the more common questions that I see or infer from the web searches&#8230;</p>
<p><strong>My Transaction Log Has Grown Too Much or My Transaction Log is Out of Space<br />
</strong></p>
<p>If you have come here because your transaction log is growing out of control there is a strong chance that you are in Full Recovery mode and are not backing up your log file on a regular basis. Your transaction log continues to grow until you explicitly backup that log (a full backup won&#8217;t do). This is the expected behavior since Full Recovery mode means you want the ability to backup to a point in time and as long as your backup is someplace safe minimize your losses to the frequency of your backups.</p>
<p><strong>Solutions:</strong></p>
<p>1.) Setup a log backup schedule that meets your business needs. Search books online and understand recovery models, figure out the SLAs you are supposed to be supporting (do it before it&#8217;s too late since you are here reading this and Mr. Murphy likes to attack in these kinds of situations). Once you figure that out get your logs backing up on that schedule. Make sure they are going to a drive other than your mdf/ldf files so they are useful in the event of a failure. Perhaps even to tape directly or after a copy. You should see your log files become more manageable size-wise, hopefully.</p>
<p>2.) Get more space. Maybe you are doing Log backups but you still don&#8217;t have enough space. Either your activity is quite high or your allocated space is quite low. If it&#8217;s the former maybe a more frequent log backup helps. If it&#8217;s the latter or former, more space for your log files may be required.</p>
<p>3.) Switch to Simple Recovery Mode. This is not to be done lightly. You are no longer able to restore to a point in time but can only restore to the last full backup. Maybe that is fine by your SLA. If it is and you have no desire to restore to a point in time, switch to simple. Your log file will now truncate (see below) on certain intervals.</p>
<p>4.) While you are adding that space or setting up your backup look at your growth ratio? Is it the default 10% for a transaction log? How large is your log file? Is 10% really the right amount you want to see it growing by? On that same note has your log file grown a lot larger than it need be because of poor management? Perhaps once you do your first T-Log backup, you should look at setting a reasonable size knowing that it will be truncated on a regular basis. If that is considerably smaller than where you are, maybe one last shrink of the log file is warranted. (Don&#8217;t shrink the database! Just the file. Here sis a good reference on <a href="http://msdn.microsoft.com/en-us/library/ms189493(SQL.90).aspx">DBCC SHRINKFILE</a> so you can see how to just shrink that one file).</p>
<p><strong>What is the Difference Between Truncate and Shrink?</strong></p>
<p>A lot of people get confused. They have truncated their log file yet they still have no free space and the file hasn&#8217;t reduced it&#8217;s footprint at all. This is because a truncation does nothing to the physical size of the allocated file on the OS. A shrink operation shrinks space from a file and a truncate essentially frees up the used space within that file.</p>
<p>This is why a shrink on a log file that is using all of the space won&#8217;t budge the size and why a truncate of a log file won&#8217;t budge the size. A truncate would have to happen first to make room available for the shrink to work. I do NOT suggest this, however. Another great post about why not to srink your log file is done by Tibor Karaszi <a href="http://www.karaszi.com/SQLServer/info_dont_shrink.asp">here</a>.</p>
<p><strong>Someone on the newsgroups told me to&#8230;<br />
</strong></p>
<p>A large portion of the folks responding to questions in the newsgroups and forums are giving great advice. They are taking their own time to help guide you down the right path. That being said, there are at times pieces of bad advice that hopefully aren&#8217;t followed in production. I talked about this practice of doing before trying in an earlier <a href="http://www.straightpathsql.com/blog/2009/1/18/empirical-evidence.html">post</a>. Gail Shaw also recently posted about this same topic on her <a href="http://sqlinthewild.co.za/index.php/2009/01/22/bad-advice/">blog.</a></p>
<p>Judging by some of the searches I have seen, it looks like people are getting some bad advice but at least trying to verify it through google (I would hope the next step is understanding the pieces involved and a test in dev/test/sandbox). A couple examples of that advice and my responses:</p>
<p><em>Stop SQL, or deatch the DB and delete the log file &#8211; </em>No. This will most assuredly remove any transactions in your log, can leave your DB in a transactionally inconsistent state meaning a potential for loss of data or worse. If you are stuck without space for further growths, try the log backup, if you must do one last truncate and shrink after making a full backup and heeding the warning to get a real recovery strategy in place.</p>
<p><em>Setup a nightly job to issue BACKUP LOG WITH TRUNCATE_ONLY &#8211; </em>I hopefully don&#8217;t need to say much more about this one that I didn&#8217;t say here or in my previous shrink post.</p>
<p>If any of this isn&#8217;t clear or you are stuck with an uncomfortable situation, send me an e-mail or leave a comment, recovery is important and I hate to see people messing this up because of bad advice or misunderstanding. I won&#8217;t charge you for any time for quick help with something like this.</p>
<p><span style="text-decoration: line-through;">This should be my last post on shrinking <img src='http://www.straightpathsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </span></p>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.straightpathsql.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.straightpathsql.com/archives/2009/01/shrinking-is-a-popular-topic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Touch that Shrink Button!</title>
		<link>http://www.straightpathsql.com/archives/2009/01/dont-touch-that-shrink-button/</link>
		<comments>http://www.straightpathsql.com/archives/2009/01/dont-touch-that-shrink-button/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 03:18:00 +0000</pubDate>
		<dc:creator>Mike Walsh</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Pet Peeve]]></category>
		<category><![CDATA[Shrinking & Transactions]]></category>
		<category><![CDATA[Syndicate]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://straightpathsql.mikewalshonline.com/?p=16</guid>
		<description><![CDATA[

This post is part 1 in a series on shrinking and transaction management. Part 2 is linked at the bottom or here. You can see them all in the Shrinking &#38; Transactions category.
This topic has indeed been done to death. Yet I still often encounter unnecessary database autoshrinks, scheduled shrink jobs and at times a seeming [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.straightpathsql.com%252Farchives%252F2009%252F01%252Fdont-touch-that-shrink-button%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FbolKPo%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Don%27t%20Touch%20that%20Shrink%20Button%21%20%23%22%20%7D);"></div>
<p><em>This post is part 1 in a series on shrinking and transaction management. Part 2 is linked at the bottom or <a href="http://www.straightpathsql.com/blog/2009/1/31/shrinking-is-a-popular-topic.html">here</a>. You can see them all in the <a href="http://www.straightpathsql.com/blog/category/shrinking-transactions">Shrinking &amp; Transactions</a> category.</em></p>
<p>This topic has indeed been done to death. Yet I still often encounter unnecessary database autoshrinks, scheduled shrink jobs and at times a seeming lack of knowledge on just what one should be doing as far as their database sizes.</p>
<p>Update [1-31-09] I wrote a follow-up to this regarding some of the questions that I see Google Search bringing folks to this blog. Check out my my additional post for some of the more common questions <a href="http://www.straightpathsql.com/blog/2009/1/31/shrinking-is-a-popular-topic.html">here</a> .</p>
<p><strong>What Happens when you Shrink a Database</strong></p>
<p>When you click that shrink button (or leave a DB in autoshrink, or schedule a job to perform shrinks), you are asking SQL Server to remove the unused space from your database&#8217;s files. Deallocate that space and let the O/S do what it needs with it. If you have a growing database (as the majority of non-static databases tend to be), this means that that database will grow again. Depending on your autogrowth settings (another pet peeve for another post) this growth will probably be more than necessary and you will end up shrinking again&#8230; At best this is just extra work (shrink grow/shrink grow) and the resulting file fragmentation is handled alright by your I/O subsystem. At worse this is causing file fragmentation, interrupting what would have otherwise been contiguous files and potentially causing I/O related performance problems.</p>
<p><strong>What should we do then, leave free space in a file?!</strong></p>
<p>Yes! In fact I find it best to plan for where your data size needs <em>will </em>be, not where they are at the time of initial go live. Set your initial database size to what you expect it to be in the future. If you can, look a year or more into the future. That&#8217;s your initial size. Create the database that size and set the autogrowth to a reasonable number in bytes rather than percent. (I hate the default of 10%.. if you have a 1TB DB that means you will be growing 100GB anytime you need to grow a file.. Better in 2005 with Instant File Initialization but still not &#8220;responsible&#8221;). Monitor your free space and look at size trending over time so you can plan for a large allocation of more space should your planning have been off. That free space in the file is &#8220;just sitting there doing nothing&#8221; as your SAN team may say, but would you rather have what you expect to need and grow into it or scramble to allocate space at the last minute? I vote for the former and if the budget allows (if you can justify it with the growth expectations, the budget better allow it because it will need to in a year anyway) it makes sense.</p>
<p><strong>Yeah, but my transaction log is always running out of space!</strong></p>
<p>Yet another pet peeve and really for another post but really quick: If you are in Full Recovery Mode on a database that means you intend to be able to recover to a point in time in the event of a failure. This means you plan on using a combination of Full Backups and Transaction Log Backups (and possibly differentials). SQL Server understands your intent, and it will not truncate (free up space within the file.. notice the file stays the same, I didn&#8217;t say <strong>shrink</strong> I said <strong>truncate</strong>.. Truncate frees space within a file, shrink removes that &#8220;free&#8221; space to make the physical file smaller) the log file(s) of your database (the .LDF files). Instead, they will continue to grow until such time as you take a transaction log backup. 90% of the time when I am helping someone with an out of control Transaction Log growth problem, it is because they are incurring the &#8220;cost&#8221; of Full Recovery mode (growing log file, the full logging of qualified events, etc.) but none of the benefit (being able to restore to a point in time from your transaction log backups if your log chain is unbroken and your log files survive)&#8230; Simple solution here.. Look at your backup/recovery plan!! Why aren&#8217;t you taking Log backups? Is it because you don&#8217;t understand them? There are plenty of resources to help. It is relatively simple to begin working on a proper backup/recovery strategy and the face you save may be your own <img src='http://www.straightpathsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Is it because you don&#8217;t need point in time recovery? Well then consider Simple Recovery mode which will truncate the log at certain events. Don&#8217;t go right to simple recovery mode though, analyze your situation and learn about recovery models and do what is right for your organization and business unit.</p>
<p><strong>Yeah, that means you shouldn&#8217;t be doing backup log with truncate_only all the time&#8230; <img src='http://www.straightpathsql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </strong></p>
<p>If this is all new to you.. Check out a couple of resources about these topics:</p>
<ul>
<li>Paul Randal has plenty to say about Shrinking Files (and says it much better than I). Check out this <a href="http://www.sqlskills.com/BLOGS/PAUL/post/Conference-Questions-Pot-Pourri-10-Shrinking-the-database-before-taking-a-backup.aspx">blog entry</a> and look at the rest of his blog.</li>
<li><a href="http://technet.microsoft.com/en-us/library/ms189275(SQL.90).aspx">Recovery Models overview</a> &#8211; Based on SQL Server 2005 but concepts apply to most versions of SQL Server</li>
</ul>
<p>As always, feel free to <a href="mailto:mike@straightpathsql.com">hit me </a>with any questions specific to your environment. I am happy to exchange quick e-mails with tips/tricks especially when it comes to my &#8220;pet peeve&#8221; topics.</p>
<p><strong>Related Posts</strong></p>
<ul>
<li><a href="http://www.straightpathsql.com/blog/2009/1/31/shrinking-is-a-popular-topic.html">Shrinking is a Popular Topic</a></li>
<li><a href="http://www.straightpathsql.com/blog/2009/4/9/manage-your-transactions-so-they-dont-manage-you-part-1.html">Manage your Transactions So they Don&#8217;t Manage You (Part 1)</a></li>
<li><a href="http://sqlserverpedia.com/wiki/Shrinking_Databases">SQLServer Pedia Article From this Blog and WebCast with Brent Ozar</a></li>
</ul>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.straightpathsql.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.straightpathsql.com/archives/2009/01/dont-touch-that-shrink-button/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
