select
B.name as TableName
,c.name IndexName
,A.OBJECT_ID,avg_fragmentation_in_percent
,' ALTER INDEX ['
+ c.name + '] ON ' + B.name +
CASE WHEN
avg_fragmentation_in_percent>= 40.0
THEN ' REBUILD'
WHEN avg_fragmentation_in_percent >= 10.0 AND
avg_fragmentation_in_percent < 40.0
THEN ' REORGANIZE'
ELSE
''
END AS
FixIndexCommand
--,A.*
from SYS.objects B
JOIN SYS.indexes C ON B.object_id=C.object_id
JOIN SYS.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,NULL) A
ON A.object_id=B.object_id AND A.database_ID = DB_ID()
AND a.index_id = C.index_id
where c.name is not null
--and B.name like 'TableName'
and A.avg_fragmentation_in_percent
>= 10.0
ORDER BY B.Name
Comments
Post a Comment