tsk138

WSUSMaintenance.sql

Mar 29th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 4.64 KB | None | 0 0
  1. /******************************************************************************
  2. This sample T-SQL script performs basic maintenance tasks on SUSDB
  3. 1. Identifies indexes that are fragmented and defragments them. For certain
  4.    tables, a fill-factor is set in order to improve insert performance.
  5.    Based on MSDN sample at http://msdn2.microsoft.com/en-us/library/ms188917.aspx
  6.    and tailored for SUSDB requirements
  7. 2. Updates potentially out-of-date table statistics.
  8. ******************************************************************************/
  9.  
  10. USE SUSDB;
  11. GO
  12. SET NOCOUNT ON;
  13.  
  14. -- Rebuild or reorganize indexes based on their fragmentation levels
  15. DECLARE @work_to_do TABLE (
  16.     objectid int
  17.     , indexid int
  18.     , pagedensity FLOAT
  19.     , fragmentation FLOAT
  20.     , numrows int
  21. )
  22.  
  23. DECLARE @objectid int;
  24. DECLARE @indexid int;
  25. DECLARE @schemaname nvarchar(130);  
  26. DECLARE @objectname nvarchar(130);  
  27. DECLARE @indexname nvarchar(130);  
  28. DECLARE @numrows int
  29. DECLARE @density FLOAT;
  30. DECLARE @fragmentation FLOAT;
  31. DECLARE @command nvarchar(4000);  
  32. DECLARE @fillfactorset bit
  33. DECLARE @numpages int
  34.  
  35. -- Select indexes that need to be defragmented based on the following
  36. -- * Page density is low
  37. -- * External fragmentation is high in relation to index size
  38. PRINT 'Estimating fragmentation: Begin. ' + CONVERT(nvarchar, getdate(), 121)  
  39. INSERT @work_to_do
  40. SELECT
  41.     f.object_id
  42.     , index_id
  43.     , avg_page_space_used_in_percent
  44.     , avg_fragmentation_in_percent
  45.     , record_count
  46. FROM  
  47.     sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'SAMPLED') AS f
  48. WHERE
  49.     (f.avg_page_space_used_in_percent < 85.0 AND f.avg_page_space_used_in_percent/100.0 * page_count < page_count - 1)
  50.     OR (f.page_count > 50 AND f.avg_fragmentation_in_percent > 15.0)
  51.     OR (f.page_count > 10 AND f.avg_fragmentation_in_percent > 80.0)
  52.  
  53. PRINT 'Number of indexes to rebuild: ' + CAST(@@ROWCOUNT AS nvarchar(20))
  54.  
  55. PRINT 'Estimating fragmentation: End. ' + CONVERT(nvarchar, getdate(), 121)
  56.  
  57. SELECT @numpages = SUM(ps.used_page_count)
  58. FROM
  59.     @work_to_do AS fi
  60.     INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id AND fi.indexid = i.index_id
  61.     INNER JOIN sys.dm_db_partition_stats AS ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id
  62.  
  63. -- Declare the cursor for the list of indexes to be processed.
  64. DECLARE curIndexes CURSOR FOR SELECT * FROM @work_to_do
  65.  
  66. -- Open the cursor.
  67. OPEN curIndexes
  68.  
  69. -- Loop through the indexes
  70. WHILE (1=1)
  71. BEGIN
  72.     FETCH NEXT FROM curIndexes
  73.     INTO @objectid, @indexid, @density, @fragmentation, @numrows;
  74.     IF @@FETCH_STATUS < 0 BREAK;
  75.  
  76.     SELECT  
  77.         @objectname = QUOTENAME(o.name)
  78.         , @schemaname = QUOTENAME(s.name)
  79.     FROM  
  80.         sys.objects AS o
  81.         INNER JOIN sys.schemas AS s ON s.schema_id = o.schema_id
  82.     WHERE  
  83.         o.object_id = @objectid;
  84.  
  85.     SELECT  
  86.         @indexname = QUOTENAME(name)
  87.         , @fillfactorset = CASE fill_factor WHEN 0 THEN 0 ELSE 1 END
  88.     FROM  
  89.         sys.indexes
  90.     WHERE
  91.         object_id = @objectid AND index_id = @indexid;
  92.  
  93.     IF ((@density BETWEEN 75.0 AND 85.0) AND @fillfactorset = 1) OR (@fragmentation < 30.0)
  94.         SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE';
  95.     ELSE IF @numrows >= 5000 AND @fillfactorset = 0
  96.         SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD WITH (FILLFACTOR = 90)';
  97.     ELSE
  98.         SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD';
  99.     PRINT CONVERT(nvarchar, getdate(), 121) + N' Executing: ' + @command;
  100.     EXEC (@command);
  101.     PRINT CONVERT(nvarchar, getdate(), 121) + N' Done.';
  102. END
  103.  
  104. -- Close and deallocate the cursor.
  105. CLOSE curIndexes;
  106. DEALLOCATE curIndexes;
  107.  
  108.  
  109. IF EXISTS (SELECT * FROM @work_to_do)
  110. BEGIN
  111.     PRINT 'Estimated number of pages in fragmented indexes: ' + CAST(@numpages AS nvarchar(20))
  112.     SELECT @numpages = @numpages - SUM(ps.used_page_count)
  113.     FROM
  114.         @work_to_do AS fi
  115.         INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id AND fi.indexid = i.index_id
  116.         INNER JOIN sys.dm_db_partition_stats AS ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id
  117.  
  118.     PRINT 'Estimated number of pages freed: ' + CAST(@numpages AS nvarchar(20))
  119. END
  120. GO
  121.  
  122.  
  123. --Update all statistics
  124. PRINT 'Updating all statistics.' + CONVERT(nvarchar, getdate(), 121)  
  125. EXEC sp_updatestats
  126. PRINT 'Done updating statistics.' + CONVERT(nvarchar, getdate(), 121)  
  127. GO
Add Comment
Please, Sign In to add comment