SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Defragmenting and rebuilding indexes


Defragmenting and rebuilding indexes

Author
Message
Cool-758033
Cool-758033
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 Visits: 94
Comments posted to this topic are about the item Defragmenting and rebuilding indexes
ssaari
ssaari
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 61
When I ran the script, I received this error:
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near '('.

Line 27:
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'LIMITED') AS i

Please help.

Thanks.

Scott
Vladimir Sotirov
Vladimir Sotirov
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 138
Hi there,
the script will be better if the current fillfactor for each index is saved and reused when the indexes are rebuild.
jswong05
jswong05
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1083 Visits: 476
You should add row-count in the whereclause because defrag on small table will not help much.

Jason
http://dbace.us
:-P
Seed Vicious
Seed Vicious
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 227
Hi, Excelent script. but i add a little if to use the enterprise on line defrag

SET NOCOUNT ON

PRINT '---------------------------------------------------------------'
PRINT 'DefragIndexes.sql script started on database [' + DB_NAME() + ']'
PRINT '---------------------------------------------------------------'
PRINT ''


-- identify tables with indexes that are fragmented above 5 percent



SELECT s.Name AS SchemaName,
o.Name AS TableName,
MAX(i.avg_fragmentation_in_percent) AS [Level]
INTO #DefragLevel
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'LIMITED') AS i
INNER JOIN sys.objects AS o
ON i.[object_id] = o.[object_id]
INNER JOIN sys.schemas AS s
ON o.[schema_id] = s.[schema_id]
WHERE i.index_id > 0
AND i.avg_fragmentation_in_percent > 5
GROUP BY s.Name,
o.Name


DECLARE @Level float,
@SchemaName sysname,
@TableName sysname,
@Command nvarchar(500)


-- process each table
DECLARE DefragCursor CURSOR FOR SELECT * FROM #DefragLevel ORDER BY [Level] DESC

OPEN DefragCursor

FETCH NEXT FROM DefragCursor INTO @SchemaName, @TableName, @Level

WHILE @@FETCH_STATUS = 0 BEGIN


-- set base command
SET @Command = N'ALTER INDEX ALL ON [' + @SchemaName + '].[' + @TableName + '] '


-- reorganize indexes below 30% fragmentation or rebuild indexes above 30%
IF @Level < 30 BEGIN
SET @Command = @Command + 'REORGANIZE'
END ELSE BEGIN
SET @Command = @Command + 'REBUILD WITH (FILLFACTOR = 70, SORT_IN_TEMPDB = ON'
END

-- DAF - a los enterprise los hace on line
if patindex('%enterprise%', convert(varchar(100), SERVERPROPERTY('edition') )) > 0
begin
SET @Command = @Command + ', ONLINE = ON)'
end
else
begin
SET @Command = @Command + ', ONLINE = OFF)'
end

-- execute command
PRINT 'Defragmenting table [' + @SchemaName + '].[' + @TableName + '] from ' + CAST(@Level AS varchar(20)) + '%...'

-- print @Command
EXEC(@Command)



-- get next table to defragment
FETCH NEXT FROM DefragCursor INTO @SchemaName, @TableName, @Level

END



-- cleanup

CLOSE DefragCursor
DEALLOCATE DefragCursor
DROP TABLE #DefragLevel



-- end process

PRINT ''
PRINT '-------------------------------------------------------------------'
PRINT 'DefragIndexes.sql script finished'
PRINT '-------------------------------------------------------------------'


Seed Vicious
Seed Vicious
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 227
ssaari (8/26/2009)
When I ran the script, I received this error:
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near '('.

Line 27:
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'LIMITED') AS i

Please help.

Thanks.

Scott


If you are using SQL 2000 (80) the script fail with this message. The same problem appears in 2005 when the master db is in level 80, may be because its a server has been updated in place from a 2000.
Iwas Bornready
Iwas Bornready
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29480 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search