giammin

Script for reducing the size of a database

Dec 14th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.29 KB | None | 0 0
  1. -- https://gist.github.com/2941270
  2.  
  3. /*
  4.  * Scripts to remove data you don't need here  
  5.  */
  6.  
  7.  
  8. /*
  9.  * Now let's clean that DB up!
  10.  */
  11.  
  12. DECLARE @DBName VarChar(25)
  13. SET @DBName = 'DBName'
  14.  
  15. /*
  16.  * Start with DBCC CLEANTABLE on the biggest offenders
  17.  */
  18.  
  19.  
  20. --http://stackoverflow.com/questions/3927231/how-can-you-tell-what-tables-are-taking-up-the-most-space-in-a-sql-server-2005-d
  21. --http://stackoverflow.com/a/3927275/18475
  22. PRINT 'Looking at the largest tables in the database.'
  23. SELECT
  24.  t.NAME AS TableName,
  25.  i.name AS indexName,
  26.  SUM(p.rows) AS RowCounts,
  27.  SUM(a.total_pages) AS TotalPages,
  28.  SUM(a.used_pages) AS UsedPages,
  29.  SUM(a.data_pages) AS DataPages,
  30.  (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB,
  31.  (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB,
  32.  (SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
  33. FROM
  34.  sys.tables t
  35. INNER JOIN  
  36.  sys.indexes i ON t.OBJECT_ID = i.object_id
  37. INNER JOIN
  38.  sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
  39. INNER JOIN
  40.  sys.allocation_units a ON p.partition_id = a.container_id
  41. WHERE
  42.  t.NAME NOT LIKE 'dt%' AND
  43.  i.OBJECT_ID > 255 AND  
  44.  i.index_id <= 1
  45. GROUP BY
  46.  t.NAME, i.object_id, i.index_id, i.name
  47. ORDER BY
  48.  OBJECT_NAME(i.object_id)
  49.  
  50.  --http://weblogs.sqlteam.com/joew/archive/2008/01/14/60456.aspx
  51. PRINT 'Cleaning the biggest offenders'
  52. DBCC CLEANTABLE(@DBName, 'dbo.Table1')
  53. DBCC CLEANTABLE(@DBName, 'dbo.Table2')
  54.  
  55. SELECT
  56.  t.NAME AS TableName,
  57.  i.name AS indexName,
  58.  SUM(p.rows) AS RowCounts,
  59.  SUM(a.total_pages) AS TotalPages,
  60.  SUM(a.used_pages) AS UsedPages,
  61.  SUM(a.data_pages) AS DataPages,
  62.  (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB,
  63.  (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB,
  64.  (SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
  65. FROM
  66.  sys.tables t
  67. INNER JOIN  
  68.  sys.indexes i ON t.OBJECT_ID = i.object_id
  69. INNER JOIN
  70.  sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
  71. INNER JOIN
  72.  sys.allocation_units a ON p.partition_id = a.container_id
  73. WHERE
  74.  t.NAME NOT LIKE 'dt%' AND
  75.  i.OBJECT_ID > 255 AND  
  76.  i.index_id <= 1
  77. GROUP BY
  78.  t.NAME, i.object_id, i.index_id, i.name
  79. ORDER BY
  80.  OBJECT_NAME(i.object_id)
  81.  
  82. /*
  83.  * Fix the Index Fragmentation and reduce the number of pages you are using (Let's rebuild and reorg those indexes)
  84.  */
  85.  
  86.  
  87. --http://ferventcoder.com/archive/2009/06/09/sql-server-2005-sql-server-2008---rebuild-or-reorganize.aspx
  88. PRINT 'Selecting Index Fragmentation in ' + @DBName + '.'
  89. SELECT
  90.   DB_NAME(DPS.DATABASE_ID) AS [DatabaseName]
  91.  ,OBJECT_NAME(DPS.OBJECT_ID) AS TableName
  92.  ,SI.NAME AS IndexName
  93.  ,DPS.INDEX_TYPE_DESC AS IndexType
  94.  ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation
  95.  ,DPS.PAGE_COUNT AS PageCounts
  96. FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, NULL) DPS --N'LIMITED') DPS
  97. INNER JOIN sysindexes SI
  98.     ON DPS.OBJECT_ID = SI.ID
  99.     AND DPS.INDEX_ID = SI.INDID
  100. ORDER BY DPS.avg_fragmentation_in_percent DESC
  101.  
  102.  
  103. PRINT 'Rebuilding indexes on every table.'
  104. EXEC sp_MSforeachtable @command1="print 'Rebuilding indexes for ?' ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR = 90)"
  105. GO
  106. PRINT 'Reorganizing indexes on every table.'
  107. EXEC sp_MSforeachtable @command1="print 'Reorganizing indexes for ?' ALTER INDEX ALL ON ? REORGANIZE"
  108. GO
  109. --EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
  110. --GO
  111. PRINT 'Updating statistics'
  112. EXEC sp_updatestats
  113. GO
  114.  
  115. SELECT
  116.   DB_NAME(DPS.DATABASE_ID) AS [DatabaseName]
  117.  ,OBJECT_NAME(DPS.OBJECT_ID) AS TableName
  118.  ,SI.NAME AS IndexName
  119.  ,DPS.INDEX_TYPE_DESC AS IndexType
  120.  ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation
  121.  ,DPS.PAGE_COUNT AS PageCounts
  122. FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, NULL) DPS --N'LIMITED') DPS
  123. INNER JOIN sysindexes SI
  124.     ON DPS.OBJECT_ID = SI.ID
  125.     AND DPS.INDEX_ID = SI.INDID
  126. ORDER BY DPS.avg_fragmentation_in_percent DESC
  127. GO
  128.  
  129. /*
  130.  * Now to really compact it down. It's likely that SHRINKDATABASE will do the work of SHRINKFILE rendering it unnecessary but it can't hurt right? Am I right?!
  131.  */
  132.  
  133. DECLARE @DBName VarChar(25), @DBFileName VarChar(25), @DBLogFileName VarChar(25)
  134. SET @DBName = 'DBName'
  135. SET @DBFileName = @DBName
  136. SET @DBLogFileName = @DBFileName + '_Log'
  137.  
  138. DBCC SHRINKFILE(@DBLogFileName,1)
  139. DBCC SHRINKFILE(@DBFileName,1)
  140. DBCC SHRINKDATABASE(@DBName,1)
Advertisement
Add Comment
Please, Sign In to add comment