索引碎片情况,表行数,批整理脚本
SELECT objects.name ,
indexes.name AS indexid ,
avg_fragmentation_in_percent AS frag ,
partition_stats.row_count,
'ALTER INDEX '+indexes.name+' ON '+objects.name+' REBUILD WITH (ONLINE = ON )'
FROM sys.dm_db_index_physical_stats(7, NULL, NULL, NULL, 'LIMITED') stats
LEFT JOIN sys.objects objects ON stats.object_id = objects.object_id
LEFT JOIN sys.indexes indexes ON indexes.index_id = stats.index_id
AND stats.object_id = indexes.object_id
LEFT JOIN sys.dm_db_partition_stats partition_stats ON stats.object_id = partition_stats.object_id
AND indexes.index_id = partition_stats.index_id
WHERE avg_fragmentation_in_percent > 4
AND stats.index_id > 0
ORDER BY row_count DESC ,
frag DESC