<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Adventures in SQL</title>
	<atom:link href="http://davidjlevy.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidjlevy.wordpress.com</link>
	<description>Random thoughts, rants, discoveries and things I wish I had done better</description>
	<lastBuildDate>Mon, 21 Dec 2009 21:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='davidjlevy.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Adventures in SQL</title>
		<link>http://davidjlevy.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://davidjlevy.wordpress.com/osd.xml" title="Adventures in SQL" />
	<atom:link rel='hub' href='http://davidjlevy.wordpress.com/?pushpress=hub'/>
		<item>
		<title></title>
		<link>http://davidjlevy.wordpress.com/2009/12/21/19/</link>
		<comments>http://davidjlevy.wordpress.com/2009/12/21/19/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 21:34:34 +0000</pubDate>
		<dc:creator>David Levy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://davidjlevy.wordpress.com/2009/12/21/19/</guid>
		<description><![CDATA[Introduction Properly managing VLFs can make or break the performance of your databases. There is a ton of information out there on the proper management of VLFs, but nothing I have found that tries to boil it down to the most important parts. So here it is, my attempt at A Busy or Accidental DBA&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=davidjlevy.wordpress.com&amp;blog=10915383&amp;post=19&amp;subd=davidjlevy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Introduction
</p>
<p>Properly managing VLFs can make or break the performance of your databases. There is a ton of information out there on the proper management of VLFs, but nothing I have found that tries to boil it down to the most important parts. So here it is, my attempt at A Busy or Accidental DBA&#8217;s Guide to Managing VLFs.
</p>
<p>What are VLFs?
</p>
<p>When SQL Server allocates new space in a log file it does it using Virtual Log Files (VLFs), meaning every growth of a transaction log file is made of 1 to many VLFs.  Think of VLFs as small files within the file that are easier for SQL Server to manage than one large file. (There really is a lot more to it than that but rather than lift from BOL I will refer you to this <a href="http://technet.microsoft.com/en-us/library/ms179355(SQL.90).aspx">page</a> for a more detailed explanation.)
</p>
<p>Why Manage VLFs?
</p>
<p>Having too many or in some cases not enough VLFs can cause sluggish database performance. I have also heard cases of database recovery taking far longer than expected when a log file contains too many VLFs.
</p>
<p>How Many VLFs Should I have?
</p>
<p>To quote someone much wiser: <a href="http://www.sqlskills.com/AboutPaulSRandal.asp">&#8220;It depends&#8221;</a>. I use 50 VLFs as my rule of thumb because it is much easier to have a simple rule and it is a safe number in most cases. I do suggest reading this article: <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/Transaction-Log-VLFs-too-many-or-too-few.aspx">Transaction Log VLFs – too many or too few?</a> before committing to a number of your own, especially if you are working with VLDBs.
</p>
<p>How do I Manage VLFs?
</p>
<p>Managing VLFs is a 2 step process. Step 1 is figuring out how many VLFs you have in each of your transaction logs. Step 2 is deciding on what number of VLFs is acceptable to you and shrinking and growing the log files to get them back under your threshold.  I have included scripts below that will help you identify and remediate high VLF counts. They probably could be wrapped up into a single script but I prefer to have control of what is running when so I can monitor for any issues the maintenance might cause.
</p>
<p>Many people also add a step 3 where they increase the auto-growth increment of their database. I tend to avoid raising the auto-growth unless the database is new. The log should only grow very rarely on a mature database; constantly having to address VLFs in a particular database&#8217;s log could be a sign of a larger problem like auto-shrink being turned on.
</p>
<p>What if I Just Shrink the Log and Let it Grow Back?
</p>
<p>There is a misconception that shrinking a log and increasing the auto-growth is enough to remediate high VLF counts. While shrinking a log file may lower VLF counts temporarily, they will come right back when the log file grows back. This article: <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/Transaction-Log-VLFs-too-many-or-too-few.aspx">Transaction Log VLFs – too many or too few?</a> lays out how many VLFs will be added based on the auto-growth increment.  Rephrased from the article:
</p>
<ul>
<li>If the file growth is less than 64MB the new portion of the log file will contain 4 VLFs
</li>
<li>If the file growth is at least 64MB and less than 1GB the new portion of the log file will contain 8 VLFs
</li>
<li>If the file growth is at least 1GB and larger = 16VLFs
</li>
</ul>
<p>Based on that, if an 80GB log with 100 VLFs was shrunk to remove VLFs then allowed to auto-grow back to 80GB with a larger auto-growth increment, say 4GB, the log would contain 20*16 = 320 VLFs.
</p>
<p>How Many VLFs are in My Databases?
</p>
<p>This script will return the VLF count for each database on the server it is run on. I am not sure of the origins of the script but I can say it works for me.  If you know or are the original author of this script please let me know so I can give proper credit or replace the script with a link to a more current version.
</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">DECLARE</span>     @query        <span style="color:blue;">varchar</span><span style="color:gray;">(</span>1000<span style="color:gray;">),<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @dbname        <span style="color:blue;">varchar</span><span style="color:gray;">(</span>1000<span style="color:gray;">),<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @count        <span style="color:blue;">int<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SET</span><br />
			<span style="color:blue;">NOCOUNT</span><br />
			<span style="color:blue;">ON<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">DECLARE</span> csr <span style="color:blue;">CURSOR</span><br />
			<span style="color:blue;">FAST_FORWARD</span><br />
			<span style="color:blue;">READ_ONLY<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">FOR</span><br />
		</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">SELECT</span>    name<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">FROM</span>    <span style="color:blue;">master</span><span style="color:gray;">.</span>dbo<span style="color:gray;">.</span><span style="color:green;">sysdatabases<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">CREATE</span><br />
			<span style="color:blue;">TABLE</span> ##loginfo<br />
</span></p>
<p><span style="color:gray;font-family:Courier New;font-size:10pt;">(<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    dbname        <span style="color:blue;">varchar</span><span style="color:gray;">(</span>100<span style="color:gray;">),<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    num_of_rows <span style="color:blue;">int</span><span style="color:gray;">)<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">OPEN</span> csr<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">FETCH</span><br />
			<span style="color:blue;">NEXT</span><br />
			<span style="color:blue;">FROM</span> csr <span style="color:blue;">INTO</span> @dbname<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:fuchsia;">@@fetch_status</span><br />
			<span style="color:gray;">&lt;&gt;</span><br />
			<span style="color:gray;">-</span>1<span style="color:gray;">)<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">BEGIN<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">CREATE</span><br />
			<span style="color:blue;">TABLE</span> #log_info<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">    </span><span style="color:gray;">(<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        fileid            <span style="color:blue;">tinyint</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        file_size        <span style="color:blue;">bigint</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        start_offset    <span style="color:blue;">bigint</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        FSeqNo            <span style="color:blue;">int</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        [status]        <span style="color:blue;">tinyint</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        parity            <span style="color:blue;">tinyint</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        create_lsn        <span style="color:blue;">numeric</span><span style="color:gray;">(</span>25<span style="color:gray;">,</span>0<span style="color:gray;">)<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:gray;">)<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">SET</span> @query <span style="color:gray;">=</span><br />
			<span style="color:red;">&#8216;DBCC loginfo (&#8216;</span><br />
			<span style="color:gray;">+</span><br />
			<span style="color:red;">&#8221;&#8221;</span><br />
			<span style="color:gray;">+</span> @dbname <span style="color:gray;">+</span><br />
			<span style="color:red;">&#8221;&#8217;)  &#8216;<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">INSERT</span><br />
			<span style="color:blue;">INTO</span> #log_info<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">EXEC </span><span style="color:gray;">(</span>@query<span style="color:gray;">)<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">SET</span> @count <span style="color:gray;">=</span><br />
			<span style="color:fuchsia;">@@rowcount<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">DROP</span><br />
			<span style="color:blue;">TABLE</span> #log_info<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">INSERT</span>    ##loginfo<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        <span style="color:blue;">VALUES</span><span style="color:gray;">(</span>@dbname<span style="color:gray;">,</span> @count<span style="color:gray;">)<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <span style="color:blue;">FETCH</span><br />
			<span style="color:blue;">NEXT</span><br />
			<span style="color:blue;">FROM</span> csr <span style="color:blue;">INTO</span> @dbname<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    <br />
</span></p>
<p><span style="color:blue;font-family:Courier New;font-size:10pt;">END<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">CLOSE</span> csr<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">DEALLOCATE</span> csr<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SELECT</span>    dbname<span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        num_of_rows<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">FROM</span>        ##loginfo<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">WHERE</span>        num_of_rows <span style="color:gray;">&gt;=</span> 50<span style="color:green;"> &#8211;My rule of thumb is 50 VLFs. Your mileage may vary.</span><br />
		</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">ORDER</span><br />
			<span style="color:blue;">BY</span>    dbname<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">DROP</span><br />
			<span style="color:blue;">TABLE</span> ##loginfo<br />
</span></p>
<p>
 </p>
<p>How Do I Lower a Database&#8217;s VLF Count?
</p>
<p>Once armed with a list of databases that have high VLF counts, the next step is to shrink the logs to as small as possible then grow them back to the original size, ideally in a single growth. This is best done during off-peak times. I wrote the following script to perform those exact steps given the appropriate USE statement. You may have to run it multiple times to get to a low enough VLF count.
</p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:green;">/*USE &lt;&lt;db_name&gt;&gt;*/</span><br />
			<span style="color:green;">&#8211;Set db name before running using drop-down above or this USE statement<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">DECLARE</span> @file_name      <span style="color:blue;">sysname</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @file_size      <span style="color:blue;">int</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @file_growth    <span style="color:blue;">int</span><span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @shrink_command <span style="color:blue;">nvarchar</span><span style="color:gray;">(</span><span style="color:fuchsia;">max</span><span style="color:gray;">),<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @alter_command  <span style="color:blue;">nvarchar</span><span style="color:gray;">(</span><span style="color:fuchsia;">max</span><span style="color:gray;">)<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SELECT</span>  @file_name <span style="color:gray;">=</span> name<span style="color:gray;">,<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @file_size <span style="color:gray;">=</span><span style="color:blue;"><br />
			</span><span style="color:gray;">(</span>size <span style="color:gray;">/</span> 128<span style="color:gray;">),<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;">        @file_growth <span style="color:gray;">=</span><br />
			<span style="color:blue;">CASE<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">WHEN    </span><span style="color:gray;">(</span>growth <span style="color:gray;">/</span> 128<span style="color:gray;">)</span><br />
			<span style="color:gray;">&lt;</span> 100<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">THEN</span>    100<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">WHEN    </span><span style="color:gray;">(</span>growth <span style="color:gray;">/</span> 128<span style="color:gray;">)</span><br />
			<span style="color:gray;">&lt;</span> 250<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">THEN</span>    250<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">WHEN    </span><span style="color:gray;">(</span>growth <span style="color:gray;">/</span> 128<span style="color:gray;">)</span><br />
			<span style="color:gray;">&lt;</span> 500<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">THEN</span>    500<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">ELSE</span>        1000<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><br />
			<span style="color:blue;">END<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">FROM</span><br />
			<span style="color:green;">sys</span><span style="color:gray;">.</span><span style="color:green;">database_files<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">WHERE</span>   type_desc <span style="color:gray;">=</span><br />
			<span style="color:red;">&#8216;log&#8217;<br />
</span></span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SELECT</span>  @shrink_command <span style="color:gray;">=</span><br />
			<span style="color:red;">&#8216;DBCC SHRINKFILE (N&#8221;&#8217;</span><br />
			<span style="color:gray;">+</span> @file_name <span style="color:gray;">+</span><br />
			<span style="color:red;">&#8221;&#8217; , 0, TRUNCATEONLY)&#8217;<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">PRINT</span>   @shrink_command<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">EXEC</span><br />
			<span style="color:maroon;">sp_executesql</span><span style="color:blue;"><br />
			</span>@shrink_command<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SELECT</span>  @shrink_command <span style="color:gray;">=</span><br />
			<span style="color:red;">&#8216;DBCC SHRINKFILE (N&#8221;&#8217;</span><br />
			<span style="color:gray;">+</span> @file_name <span style="color:gray;">+</span><br />
			<span style="color:red;">&#8221;&#8217; , 0)&#8217;<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">PRINT</span>   @shrink_command<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">EXEC</span><br />
			<span style="color:maroon;">sp_executesql</span><span style="color:blue;"><br />
			</span>@shrink_command<br />
</span></p>
<p>
 </p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">SELECT</span>  @alter_command <span style="color:gray;">=</span><br />
			<span style="color:red;">&#8216;ALTER DATABASE ['</span><br />
			<span style="color:gray;">+</span><br />
			<span style="color:fuchsia;">db_name</span><span style="color:gray;">()</span><br />
			<span style="color:gray;">+</span><br />
			<span style="color:red;">'] MODIFY FILE (NAME = N&#8221;&#8217;</span><br />
			<span style="color:gray;">+</span> @file_name <span style="color:gray;">+</span><br />
			<span style="color:red;">&#8221;&#8217;, SIZE = &#8216;</span><br />
			<span style="color:gray;">+</span><br />
			<span style="color:fuchsia;">CAST</span><span style="color:gray;">(</span>@file_size <span style="color:blue;">AS</span><br />
			<span style="color:blue;">nvarchar</span><span style="color:gray;">)</span><br />
			<span style="color:gray;">+</span><br />
			<span style="color:red;">&#8216;MB)&#8217;<br />
</span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">PRINT</span>   @alter_command<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">EXEC</span><br />
			<span style="color:maroon;">sp_executesql</span><span style="color:blue;"><br />
			</span>@alter_command<br />
</span></p>
<p>In Closing
</p>
<p>This has by no means a comprehensive lesson in VLFs or transaction log management, but hopefully enough to get the job done. If you are looking for a more in-depth look at VLFs and transaction logs in general I suggest reading the following articles: <a href="http://technet.microsoft.com/en-us/magazine/2009.02.logging.aspx">Understanding Logging and Recovery in SQL Server</a>, <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/Transaction-Log-VLFs-too-many-or-too-few.aspx">Transaction Log VLFs – too many or too few?</a> and <a href="http://sqlskills.com/blogs/kimberly/post/8-Steps-to-better-Transaction-Log-throughput.aspx">8 Steps to better Transaction Log throughput</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/davidjlevy.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/davidjlevy.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/davidjlevy.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=davidjlevy.wordpress.com&amp;blog=10915383&amp;post=19&amp;subd=davidjlevy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://davidjlevy.wordpress.com/2009/12/21/19/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a34b32452cd553634cc6040f9f2ab4b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">davilev</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Shrink TempDB in SQL 2005</title>
		<link>http://davidjlevy.wordpress.com/2009/12/11/how-to-shrink-tempdb-in-sql-2005/</link>
		<comments>http://davidjlevy.wordpress.com/2009/12/11/how-to-shrink-tempdb-in-sql-2005/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 23:14:24 +0000</pubDate>
		<dc:creator>David Levy</dc:creator>
				<category><![CDATA[DBCC]]></category>
		<category><![CDATA[DBCC FREEPROCCACHE]]></category>
		<category><![CDATA[DBCC SHRINKFILE]]></category>
		<category><![CDATA[Shrink]]></category>
		<category><![CDATA[TempDB]]></category>

		<guid isPermaLink="false">http://davidjlevy.wordpress.com/?p=4</guid>
		<description><![CDATA[Introduction  From time to time you find yourself needing to shrink some space out of TempDB. Shrinking database files is never my first choice but sometimes it is the best I have. Many people think that you cannot shrink TempDB in SQL 2005, but I am going to show you how.   Why would I need [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=davidjlevy.wordpress.com&amp;blog=10915383&amp;post=4&amp;subd=davidjlevy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;"><strong>Introduction</strong></span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">From time to time you find yourself needing to shrink some space out of TempDB. Shrinking database files is never my first choice but sometimes it is the best I have. Many people think that you cannot shrink TempDB in SQL 2005, but I am going to show you how. </span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;"><strong>Why would I need to shrink TempDB?</strong></span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">Yesterday afternoon my pager started going crazy because an Ad-Hoc query that needed some tuning filled TempDB on a server. Luckily, the user only impacted their own query so it was easy to quickly identify them and work with the right people to get the query rewritten.</span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">Once the immediate problem was resolved there had to be some cleanup. On this server, TempDB has 32 files (1 per processor) all on the same disk. The full database condition caused all kinds of alerts in our monitoring tools, from drive space alerts to too few growths remaining. There were 3 possible solutions to quiet the alerts:</span> </p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="line-height:115%;font-size:10pt;"><span style="font-family:Calibri;">1.</span><span style="font:7pt 'Times New Roman';">       </span></span><span style="font-family:Calibri;font-size:small;">Reboot – There is never a good time to reboot a production server</span> </p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="line-height:115%;font-size:10pt;"><span style="font-family:Calibri;">2.</span><span style="font:7pt 'Times New Roman';">       </span></span><span style="font-family:Calibri;font-size:small;">Turn off the Alerts – Not really an option. My preference would be for increasing the sensitivity</span> </p>
<p class="MsoListParagraphCxSpLast" style="text-indent:-.25in;margin:0 0 10pt .5in;"><span style="line-height:115%;font-size:10pt;"><span style="font-family:Calibri;">3.</span><span style="font:7pt 'Times New Roman';">       </span></span><span style="font-family:Calibri;font-size:small;">Shrink TempDB – Not a great option, but the best of the 3</span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;"><strong>Shrinking TempDB</strong></span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">Once we had decided that we would go ahead and shrink the files in TempDB it seemed like the hard part was done, but after r</span><span style="font-family:Calibri;font-size:small;">unning the following command:</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">USE</span><span style="font-family:'Courier New';font-size:10pt;"> [tempdb]</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">GO</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">DBCC</span><span style="font-family:'Courier New';font-size:10pt;"> SHRINKFILE<span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">N&#8217;tempdev&#8217;</span> <span style="color:gray;">,</span> 5000<span style="color:gray;">)</span></span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="line-height:115%;font-family:'Courier New';color:blue;font-size:10pt;">GO</span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">I got back the following:</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">DBCC SHRINKFILE: Page 1:878039 could not be moved because it is a work file page.</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">DbId   FileId      CurrentSize MinimumSize UsedPages   EstimatedPages</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">&#8212;&#8212; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8211;</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">2      1           878040      640000      4672        4672</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;"> </span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">(1 row(s) affected)</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;"> </span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;"> </span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">“Page could not be moved because it is a work file page.”…grrr. This is a new thing in SQL 2005 caused by the caching that is done in TempDB. I am not going to try to explain here how objects are cached in TempDB, but Kalen Delaney’s Inside Sql Server Series is a great place to learn about it if you are interested (</span><a href="http://www.insidesqlserver.com/books.html"><span style="font-family:Calibri;color:#800080;font-size:small;">http://www.insidesqlserver.com/books.html</span></a><span style="font-family:Calibri;font-size:small;">).  What is important is that the cached objects are tied to a query plan and that by freeing the procedure cache you can make those objects go away, allowing you to shrink your files.</span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-family:Calibri;font-size:small;">Trying again:</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">DBCC</span><span style="font-family:'Courier New';font-size:10pt;"> FREEPROCCACHE</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">GO</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">USE</span><span style="font-family:'Courier New';font-size:10pt;"> [tempdb]</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">GO</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';color:blue;font-size:10pt;">DBCC</span><span style="font-family:'Courier New';font-size:10pt;"> SHRINKFILE<span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">N&#8217;tempdev&#8217;</span> <span style="color:gray;">,</span> 5000<span style="color:gray;">)</span></span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="line-height:115%;font-family:'Courier New';color:blue;font-size:10pt;">GO</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Calibri;font-size:small;">This time it worked:</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Calibri;font-size:small;"> </span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">DbId   FileId      CurrentSize MinimumSize UsedPages   EstimatedPages</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">&#8212;&#8212; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8211;</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">2      1           640000      640000      264         264</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;"> </span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;">(1 row(s) affected)</span> </p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:'Courier New';font-size:8pt;"> </span> </p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="line-height:115%;font-family:'Courier New';font-size:8pt;">DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span> </p>
<p><span style="font-family:Calibri,sans-serif;font-size:11pt;">I think I got lucky that the shrink worked on the first try. There will certainly be times when you have to try freeing the procedure cache and shrinking multiple times to get a file to shrink, but eventually it will get the job done.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/davidjlevy.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/davidjlevy.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/davidjlevy.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=davidjlevy.wordpress.com&amp;blog=10915383&amp;post=4&amp;subd=davidjlevy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://davidjlevy.wordpress.com/2009/12/11/how-to-shrink-tempdb-in-sql-2005/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<georss:point>0.000000 0.000000</georss:point>
		<geo:lat>0.000000</geo:lat>
		<geo:long>0.000000</geo:long>
		<media:content url="http://0.gravatar.com/avatar/a34b32452cd553634cc6040f9f2ab4b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">davilev</media:title>
		</media:content>
	</item>
	</channel>
</rss>
