Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
houghjohnny
Starting Member
4 Posts |
Posted - 2011-06-06 : 19:05:13
|
| I need help modifying the following script to give me the time it takes for each individual index to be built as opposed to the cumulative time for all indexesSET NOCOUNT ONGO--Set the fillfactorDECLARE @FillFactor TINYINTSELECT @FillFactor=80DECLARE @StartTime DATETIMESELECT @StartTime=GETDATE()if object_id('tempdb..#TablesToRebuildIndex') is not nullbegindrop table #TablesToRebuildIndexendDECLARE @NumTables VARCHAR(20)SELECTs.[Name] AS SchemaName,t.[name] AS TableName,SUM(p.rows) AS RowsInTableINTO #TablesToRebuildIndexFROMsys.schemas sLEFT JOIN sys.tables tON s.schema_id = t.schema_idLEFT JOIN sys.partitions pON t.object_id = p.object_idLEFT JOIN sys.allocation_units aON p.partition_id = a.container_idWHEREp.index_id IN ( 0, 1 ) -- 0 heap table , 1 table with clustered indexAND p.rows IS NOT NULLAND a.type = 1 -- row-data only , not LOBGROUP BYs.[Name],t.[name]SELECT @NumTables=@@ROWCOUNTDECLARE RebuildIndex CURSOR FORSELECTROW_NUMBER() OVER (ORDER BY ttus.RowsInTable),ttus.SchemaName,ttus.TableName,ttus.RowsInTableFROM#TablesToRebuildIndex AS ttusORDER BYttus.RowsInTableOPEN RebuildIndexDECLARE @TableNumber VARCHAR(20)DECLARE @SchemaName NVARCHAR(128)DECLARE @tableName NVARCHAR(128)DECLARE @RowsInTable VARCHAR(20)DECLARE @Statement NVARCHAR(300)DECLARE @Status NVARCHAR(300)FETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTableWHILE ( @@FETCH_STATUS = 0 )BEGINSET @Status='Table '+@TableNumber+' of '+@NumTables+': Rebuilding indexes on '+@SchemaName+'.'+@tablename + ' ('+@RowsInTable+' rows)'RAISERROR (@Status, 0, 1) WITH NOWAIT --RAISERROR used to immediately output statusSET @Statement = 'ALTER INDEX ALL ON ['+@SchemaName+'].['+@tablename +'] REBUILD WITH (FILLFACTOR = '+CONVERT(VARCHAR(3), @FillFactor)+' )'EXEC sp_executesql @StatementPRINT @StatementFETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTableENDCLOSE RebuildIndexDEALLOCATE RebuildIndexdrop table #TablesToRebuildIndexPrint 'Total Elapsed Time: '+ CONVERT(VARCHAR(100), DATEDIFF(second, @StartTime, GETDATE()))+' seconds'GO |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|