<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Rob Reynolds - The Fervent Coder : Database</title><link>http://devlicio.us/blogs/rob_reynolds/archive/tags/Database/default.aspx</link><description>Tags: Database</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Super D to the B to the A – AKA Script for reducing the size of a database</title><link>http://devlicio.us/blogs/rob_reynolds/archive/2012/12/14/super-d-to-the-b-to-the-a-aka-script-for-reducing-the-size-of-a-database.aspx</link><pubDate>Fri, 14 Dec 2012 15:50:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:70697</guid><dc:creator>Rob Reynolds</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/rob_reynolds/rsscomments.aspx?PostID=70697</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/rob_reynolds/commentapi.aspx?PostID=70697</wfw:comment><comments>http://devlicio.us/blogs/rob_reynolds/archive/2012/12/14/super-d-to-the-b-to-the-a-aka-script-for-reducing-the-size-of-a-database.aspx#comments</comments><description>&lt;p&gt;The following is a script that I used to help me clean up a database and reduce the size of it from 95MB down to 3MB so we could use it for a development backup. I will note that we also removed some of the data out. I shared this with a friend recently and he used this to go &lt;a href="https://twitter.com/seifattar/status/278901275620438016"&gt;from 70GB to 7GB&lt;/a&gt;! &lt;/p&gt;  &lt;h4&gt;UPDATE: Special Note&lt;/h4&gt;  &lt;p&gt;Please don’t run this against something that is live or performance critical. You want to do this where you are the only person connected to the database, like a restored backup of the critical database. Doing it against something live will most definitely cause issues. I can in no way be responsible for the use of this script. You should understand what you are doing before you execute these scripts.&lt;/p&gt;  &lt;h4&gt;So what does it do? &lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;It gives you a report of what tables are taking up the most space. &lt;/li&gt;    &lt;li&gt;It allows you to specify those tables for cleaning. &lt;/li&gt;    &lt;li&gt;Gives you that same report of space used up by tables after the clean. &lt;/li&gt;    &lt;li&gt;It rebuilds and reorganizes all indexes with reports before and after. &lt;/li&gt;    &lt;li&gt;It runs shrink file on the physical files (potentially unnecessary due to the next thing it does, but hey, couldn’t hurt right?!). &lt;/li&gt;    &lt;li&gt;It runs shrink database on the database. &lt;/li&gt; &lt;/ul&gt;  &lt;h4&gt;The Script&lt;/h4&gt;  &lt;p&gt;Provided it shows up correctly, here is the &lt;a href="https://gist.github.com/2941270"&gt;gist&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="sql" name="code"&gt;/*
 * Scripts to remove data you don&amp;#39;t need here  
 */


/*
 * Now let&amp;#39;s clean that DB up!
 */

DECLARE @DBName VarChar(25)
SET @DBName = &amp;#39;DBName&amp;#39;

/*
 * Start with DBCC CLEANTABLE on the biggest offenders
 */


--http://stackoverflow.com/questions/3927231/how-can-you-tell-what-tables-are-taking-up-the-most-space-in-a-sql-server-2005-d
--http://stackoverflow.com/a/3927275/18475
PRINT &amp;#39;Looking at the largest tables in the database.&amp;#39;
SELECT 
 t.NAME AS TableName,
 i.name AS indexName,
 SUM(p.rows) AS RowCounts,
 SUM(a.total_pages) AS TotalPages, 
 SUM(a.used_pages) AS UsedPages, 
 SUM(a.data_pages) AS DataPages,
 (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB, 
 (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB, 
 (SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM 
 sys.tables t
INNER JOIN  
 sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
 sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
 sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
 t.NAME NOT LIKE &amp;#39;dt%&amp;#39; AND
 i.OBJECT_ID &amp;gt; 255 AND  
 i.index_id &amp;lt;= 1
GROUP BY 
 t.NAME, i.object_id, i.index_id, i.name 
ORDER BY 
 OBJECT_NAME(i.object_id) 

 --http://weblogs.sqlteam.com/joew/archive/2008/01/14/60456.aspx
PRINT &amp;#39;Cleaning the biggest offenders&amp;#39;
DBCC CLEANTABLE(@DBName, &amp;#39;dbo.Table1&amp;#39;)
DBCC CLEANTABLE(@DBName, &amp;#39;dbo.Table2&amp;#39;)

SELECT 
 t.NAME AS TableName,
 i.name AS indexName,
 SUM(p.rows) AS RowCounts,
 SUM(a.total_pages) AS TotalPages, 
 SUM(a.used_pages) AS UsedPages, 
 SUM(a.data_pages) AS DataPages,
 (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB, 
 (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB, 
 (SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM 
 sys.tables t
INNER JOIN  
 sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
 sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
 sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
 t.NAME NOT LIKE &amp;#39;dt%&amp;#39; AND
 i.OBJECT_ID &amp;gt; 255 AND  
 i.index_id &amp;lt;= 1
GROUP BY 
 t.NAME, i.object_id, i.index_id, i.name 
ORDER BY 
 OBJECT_NAME(i.object_id) 

/*
 * Fix the Index Fragmentation and reduce the number of pages you are using (Let&amp;#39;s rebuild and reorg those indexes)
 */


--http://ferventcoder.com/archive/2009/06/09/sql-server-2005-sql-server-2008---rebuild-or-reorganize.aspx 
PRINT &amp;#39;Selecting Index Fragmentation in &amp;#39; + @DBName + &amp;#39;.&amp;#39;
SELECT 
  DB_NAME(DPS.DATABASE_ID) AS [DatabaseName]
 ,OBJECT_NAME(DPS.OBJECT_ID) AS TableName
 ,SI.NAME AS IndexName
 ,DPS.INDEX_TYPE_DESC AS IndexType
 ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation
 ,DPS.PAGE_COUNT AS PageCounts
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, NULL) DPS --N&amp;#39;LIMITED&amp;#39;) DPS
INNER JOIN sysindexes SI 
    ON DPS.OBJECT_ID = SI.ID 
    AND DPS.INDEX_ID = SI.INDID
ORDER BY DPS.avg_fragmentation_in_percent DESC


PRINT &amp;#39;Rebuilding indexes on every table.&amp;#39;
EXEC sp_MSforeachtable @command1=&amp;quot;print &amp;#39;Rebuilding indexes for ?&amp;#39; ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR = 90)&amp;quot;
GO
PRINT &amp;#39;Reorganizing indexes on every table.&amp;#39;
EXEC sp_MSforeachtable @command1=&amp;quot;print &amp;#39;Reorganizing indexes for ?&amp;#39; ALTER INDEX ALL ON ? REORGANIZE&amp;quot;
GO
--EXEC sp_MSforeachtable @command1=&amp;quot;print &amp;#39;?&amp;#39; DBCC DBREINDEX (&amp;#39;?&amp;#39;, &amp;#39; &amp;#39;, 80)&amp;quot;
--GO
PRINT &amp;#39;Updating statistics&amp;#39;
EXEC sp_updatestats
GO

SELECT 
  DB_NAME(DPS.DATABASE_ID) AS [DatabaseName]
 ,OBJECT_NAME(DPS.OBJECT_ID) AS TableName
 ,SI.NAME AS IndexName
 ,DPS.INDEX_TYPE_DESC AS IndexType
 ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation
 ,DPS.PAGE_COUNT AS PageCounts
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, NULL) DPS --N&amp;#39;LIMITED&amp;#39;) DPS
INNER JOIN sysindexes SI 
    ON DPS.OBJECT_ID = SI.ID 
    AND DPS.INDEX_ID = SI.INDID
ORDER BY DPS.avg_fragmentation_in_percent DESC
GO

/*
 * Now to really compact it down. It&amp;#39;s likely that SHRINKDATABASE will do the work of SHRINKFILE rendering it unnecessary but it can&amp;#39;t hurt right? Am I right?!
 */

DECLARE @DBName VarChar(25), @DBFileName VarChar(25), @DBLogFileName VarChar(25)
SET @DBName = &amp;#39;DBName&amp;#39;
SET @DBFileName = @DBName
SET @DBLogFileName = @DBFileName + &amp;#39;_Log&amp;#39;

DBCC SHRINKFILE(@DBLogFileName,1)
DBCC SHRINKFILE(@DBFileName,1)
DBCC SHRINKDATABASE(@DBName,1) &lt;/pre&gt;

&lt;h4&gt;References&lt;/h4&gt;

&lt;p&gt;Here are some of the references in the gist:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://stackoverflow.com/a/3927275/18475"&gt;http://stackoverflow.com/a/3927275/18475&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://stackoverflow.com/questions/3927231/how-can-you-tell-what-tables-are-taking-up-the-most-space-in-a-sql-server-2005-d"&gt;http://stackoverflow.com/questions/3927231/how-can-you-tell-what-tables-are-taking-up-the-most-space-in-a-sql-server-2005-d&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://ferventcoder.com/archive/2009/06/09/sql-server-2005-sql-server-2008---rebuild-or-reorganize.aspx"&gt;http://ferventcoder.com/archive/2009/06/09/sql-server-2005-sql-server-2008---rebuild-or-reorganize.aspx&lt;/a&gt;&amp;#160; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://weblogs.sqlteam.com/joew/archive/2008/01/14/60456.aspx"&gt;http://weblogs.sqlteam.com/joew/archive/2008/01/14/60456.aspx&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=70697" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/rob_reynolds/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/rob_reynolds/archive/tags/Database/default.aspx">Database</category></item></channel></rss>