snootalope
IS-IT--Management
I copied this script from an online source to tell me which index's I may need to defrag:
SELECT OBJECT_NAME(i.object_id) AS TableName,
i.name AS IndexName,
indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL,'DETAILED') indexstats
INNER JOIN sys.indexes i ON i.object_id = indexstats.object_id
AND i.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent > 20
However, when I run it I get:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '('.
I can't for the life of me figure it out...
Long story short, I issued the following to defrag all the indexs:
EXEC [sp_MSforeachtable] @command1="RAISERROR('DBCC DBREINDEX(''?'') ...',10,1) WITH NOWAIT DBCC DBREINDEX('?')"
Once it was done, I issued:
SELECT * FROM sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, NULL);
And still, objects with more than 1000 pages still have fragmentation, some with percents as high as 99%!!
Our users overall SQL sessions are just starting to drag and I've got to figure out how to defrag these index's to get them back into shape..
help?
SELECT OBJECT_NAME(i.object_id) AS TableName,
i.name AS IndexName,
indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL,'DETAILED') indexstats
INNER JOIN sys.indexes i ON i.object_id = indexstats.object_id
AND i.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent > 20
However, when I run it I get:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '('.
I can't for the life of me figure it out...
Long story short, I issued the following to defrag all the indexs:
EXEC [sp_MSforeachtable] @command1="RAISERROR('DBCC DBREINDEX(''?'') ...',10,1) WITH NOWAIT DBCC DBREINDEX('?')"
Once it was done, I issued:
SELECT * FROM sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, NULL);
And still, objects with more than 1000 pages still have fragmentation, some with percents as high as 99%!!
Our users overall SQL sessions are just starting to drag and I've got to figure out how to defrag these index's to get them back into shape..
help?