Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Defragmenting and rebuilding indexes Expand / Collapse
Author
Message
Posted Thursday, August 13, 2009 9:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 20, 2014 4:11 AM
Points: 53, Visits: 94
Comments posted to this topic are about the item Defragmenting and rebuilding indexes
Post #770288
Posted Wednesday, August 26, 2009 1:27 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 15, 2012 2:49 PM
Points: 10, 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
Post #777802
Posted Monday, September 28, 2009 5:51 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 5:43 PM
Points: 799, Visits: 125
Hi there,
the script will be better if the current fillfactor for each index is saved and reused when the indexes are rebuild.
Post #794843
Posted Tuesday, December 8, 2009 3:03 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 22, 2014 10:17 AM
Points: 27, Visits: 467
You should add row-count in the whereclause because defrag on small table will not help much.

Jason
http://dbace.us
Post #831055
Posted Wednesday, January 9, 2013 5:29 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 14, 2014 12:45 PM
Points: 21, Visits: 225
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 '-------------------------------------------------------------------'






Post #1404710
Posted Wednesday, January 9, 2013 5:32 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 14, 2014 12:45 PM
Points: 21, Visits: 225
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.

Post #1404712
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse