Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. /*
  2. MSSQL
  3. -----
  4. Rebuilds indexes on smallest tables first, allowing the maximum number of indexes to be rebuilt in the shortest amount of time.
  5. Real time progress updates, allowing you to estimate how much time is remaining before completion.
  6. Correctly handles multiple schemas, a common flaw in other scripts.
  7. */
  8. SET NOCOUNT ON
  9. GO
  10.  
  11. --Set the fillfactor
  12. DECLARE @FillFactor TINYINT
  13. SELECT @FillFactor=80
  14.  
  15. DECLARE @StartTime DATETIME
  16. SELECT @StartTime=GETDATE()
  17.  
  18. if object_id('tempdb..#TablesToRebuildIndex') is not null
  19. begin
  20. drop table #TablesToRebuildIndex
  21. end
  22.  
  23. DECLARE @NumTables VARCHAR(20)
  24.  
  25. SELECT
  26. s.[Name] AS SchemaName,
  27. t.[name] AS TableName,
  28. SUM(p.rows) AS RowsInTable
  29. INTO #TablesToRebuildIndex
  30. FROM
  31. sys.schemas s
  32. LEFT JOIN sys.tables t
  33. ON s.schema_id = t.schema_id
  34. LEFT JOIN sys.partitions p
  35. ON t.object_id = p.object_id
  36. LEFT JOIN sys.allocation_units a
  37. ON p.partition_id = a.container_id
  38. WHERE
  39. p.index_id IN ( 0, 1 ) -- 0 heap table , 1 table with clustered index
  40. AND p.rows IS NOT NULL
  41. AND a.type = 1 -- row-data only , not LOB
  42. GROUP BY
  43. s.[Name],
  44. t.[name]
  45. SELECT @NumTables=@@ROWCOUNT
  46.  
  47. DECLARE RebuildIndex CURSOR FOR
  48. SELECT
  49. ROW_NUMBER() OVER (ORDER BY ttus.RowsInTable),
  50. ttus.SchemaName,
  51. ttus.TableName,
  52. ttus.RowsInTable
  53. FROM
  54. #TablesToRebuildIndex AS ttus
  55. ORDER BY
  56. ttus.RowsInTable
  57. OPEN RebuildIndex
  58.  
  59. DECLARE @TableNumber VARCHAR(20)
  60. DECLARE @SchemaName NVARCHAR(128)
  61. DECLARE @tableName NVARCHAR(128)
  62. DECLARE @RowsInTable VARCHAR(20)
  63. DECLARE @Statement NVARCHAR(300)
  64. DECLARE @Status NVARCHAR(300)
  65.  
  66. FETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTable
  67. WHILE ( @@FETCH_STATUS = 0 )
  68. BEGIN
  69. SET @Status='Table '+@TableNumber+' of '+@NumTables+': Rebuilding indexes on '+@SchemaName+'.'+@tablename + ' ('+@RowsInTable+' rows)'
  70. RAISERROR (@Status, 0, 1) WITH NOWAIT --RAISERROR used to immediately output status
  71.  
  72. SET @Statement = 'ALTER INDEX ALL ON ['+@SchemaName+'].['+@tablename +'] REBUILD WITH (FILLFACTOR = '+CONVERT(VARCHAR(3), @FillFactor)+' )'
  73. EXEC sp_executesql @Statement
  74.  
  75. FETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTable
  76. END
  77.  
  78. CLOSE RebuildIndex
  79. DEALLOCATE RebuildIndex
  80.  
  81. drop table #TablesToRebuildIndex
  82.  
  83. Print 'Total Elapsed Time: '+CONVERT(VARCHAR(100), DATEDIFF(minute, @StartTime, GETDATE()))+' minutes'
  84.  
  85. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement