<?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>Makl Ndrix &#187; db logs</title>
	<atom:link href="http://michaelhendrickx.com/tag/db-logs/feed" rel="self" type="application/rss+xml" />
	<link>http://michaelhendrickx.com</link>
	<description>may contain traces of nuts</description>
	<lastBuildDate>Thu, 17 May 2012 21:03:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to shrink Microsoft SQL log files</title>
		<link>http://michaelhendrickx.com/201007_how-to-shrink-microsoft-sql-log-files.html</link>
		<comments>http://michaelhendrickx.com/201007_how-to-shrink-microsoft-sql-log-files.html#comments</comments>
		<pubDate>Mon, 12 Jul 2010 04:55:58 +0000</pubDate>
		<dc:creator>Michael Hendrickx</dc:creator>
				<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[db logs]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://michaelhendrickx.com/?p=302</guid>
		<description><![CDATA[When having Microsoft SQL databases, its log files can grow quite a bit, potentially slow down the database server and eat up disk space. To shrink a database, one can run the following line: EXEC D_ShrinkDBLogs 0,100,1000,'with truncate_only','DB_NAME' (change DB_NAME with the database&#8217;s name) To shrink all databases, one can use &#8220;sp_MSforeachdb&#8221; which is an [...]<div class="addthis_toolbox addthis_default_style " addthis:url='http://michaelhendrickx.com/201007_how-to-shrink-microsoft-sql-log-files.html' addthis:title='How to shrink Microsoft SQL log files'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p><img src="http://michaelhendrickx.com/wp-content/uploads/2010/07/nsp1.jpg" alt="" title="nsp" width="242" height="201" align="right" /></p>
<p>When having Microsoft SQL databases, its log files can grow quite a bit, potentially slow down the database server and eat up disk space.  </p>
<p>To shrink a database, one can run the following line: </p>
<p><strong><code>EXEC D_ShrinkDBLogs 0,100,1000,'with truncate_only','DB_NAME'</code></strong><br />
(change DB_NAME with the database&#8217;s name)</p>
<p>To shrink all databases, one can use &#8220;sp_MSforeachdb&#8221; which is an undocumented sql stored procedure:</p>
<p><strong><code>EXEC sp_MSForEachDB 'D_ShrinkDBLogs 0,100,1000,''with truncate_only'',''?'''</code></strong></p>
<p>To run this, the following stored procedure need to be installed. It can just be copy pasted from the code below (or download from <a href='http://michaelhendrickx.com/wp-content/uploads/2010/07/d_shrinkdblogs.txt'>this</a> link):<br />
<span id="more-302"></span></p>
<pre>
CREATE PROC [dbo].[D_ShrinkDBLogs]
  @target_percent TINYINT = 0, @target_size_MB INT,  @max_iterations INT, @trunc NVARCHAR(1000),
  @DB VARCHAR(30)
AS
  SET nocount ON
  SET @trunc = 'with truncate_only'
  --SET @trunc = ''
  DECLARE
      @last_row INT, @log_size DECIMAL(15,2), @unused_at_start DECIMAL(15,2),
      @unused DECIMAL(15,2), @shrinkable DECIMAL(15,2), @iteration INT, @file_max INT,
      @file INT, @fileid VARCHAR(5)

  SELECT @iteration = 0
  PRINT @DB
  CREATE TABLE #loginfo
     (id INT IDENTITY, FileId INT, FileSize NUMERIC(22,0), StartOffset NUMERIC(22,0),
       FSeqNo INT, Status INT, Parity SMALLINT, CreateLSN NUMERIC(30) )

  CREATE TABLE #logfiles
     (id INT IDENTITY(1,1), fileid VARCHAR(5) NOT NULL )
  INSERT #logfiles
    ( fileid )
     SELECT CONVERT(VARCHAR,fileid) FROM sysfiles WHERE status = 1048642
     SELECT @file_max = @@rowcount

  IF OBJECT_ID('shrinktab') IS NULL
    EXEC ('create table shrinktab ( x nchar(3000) not null )')
  INSERT #loginfo
     (
      FileId,FileSize,StartOffset,FSeqNo,Status,
      Parity,CreateLSN )
      EXEC ('dbcc loginfo (' + @DB + ')'
     )
  SELECT @last_row = @@rowcount
  PRINT @last_row
  SELECT
      @log_size = SUM(FileSize) / 1048576.00,
      @unused = SUM(CASE WHEN Status = 0
      THEN FileSize ELSE 0 END) / 1048576.00,

  @shrinkable = SUM(CASE WHEN id < @last_row - 1 AND Status = 0
          THEN FileSize
          ELSE 0
          END) / 1048576.00
          FROM #loginfo

  SELECT @unused_at_start = @unused -- save for later
  SELECT 'iteration' = @iteration, 'log size, MB' = @log_size,
  'unused log, MB' = @unused, 'shrinkable log, MB' = @shrinkable,
   'shrinkable %' = CONVERT(DECIMAL(6,2), @shrinkable * 100 / @log_size)
  WHILE @shrinkable * 100 / @log_size > @target_percent
    AND @shrinkable > @target_size_MB
    AND @iteration < @max_iterations
    BEGIN
      SELECT @iteration = @iteration + 1 -- this is just a precaution
      EXEC ('insert shrinktab select name from sysobjects delete shrinktab')
      SELECT @file = 0
      WHILE @file < @file_max
        BEGIN
          SELECT @file = @file + 1
          SELECT @fileid = fileid FROM #logfiles WHERE id = @file
          EXEC
          ('use ' + @DB + '; dbcc shrinkfile( ' + @fileid + ' )')
        END
      EXEC
       ( 'backup log ' + @db + ' ' + @trunc   )

      TRUNCATE TABLE #loginfo
      INSERT #loginfo
       ( FileId,FileSize,StartOffset, FSeqNo,Status,Parity,CreateLSN )
       EXEC ( 'dbcc loginfo' )
      SELECT @last_row = @@rowcount
      SELECT @log_size = SUM(FileSize) / 1048576.00,
       @unused = SUM(CASE WHEN Status = 0 THEN FileSize
       ELSE 0
       END) / 1048576.00,
       @shrinkable = SUM(CASE WHEN id < @last_row - 1
       AND Status = 0 THEN FileSize
       ELSE 0
       END)
       / 1048576.00
       FROM #loginfo

       SELECT 'iteration' = @iteration, 'log size, MB' = @log_size,
       'unused log, MB' = @unused, 'shrinkable log, MB' = @shrinkable,
       'shrinkable %' = CONVERT(DECIMAL(6,2),@shrinkable
       * 100 / @log_size)
    END
 SELECT CONVERT(VARCHAR,@iteration) + ' iterations. Log shrunk from ' +
 CONVERT(VARCHAR,@unused_at_start) + ' MB to ' + CONVERT(VARCHAR,@unused) + ' MB'
 EXEC ( 'drop table shrinktab' )
</pre>
<p>Note that this procedure wasn't written by me, but ramassed together by a good friend of mine.</p>
<p>Thank you,<br />
Michael</p>
]]></content:encoded>
			<wfw:commentRss>http://michaelhendrickx.com/201007_how-to-shrink-microsoft-sql-log-files.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

