我找到了这个脚本,它将使用系统函数sys.dm_db_index_physical_stats
分析数据库的碎片,并给出给定数据库的objectID,IndexID和%Fragmentation,但我想知道该表名称和索引名称而不是ID。
脚本在此处找到:Defragmenting Indexes in SQL Server 2005 & 2008
查询本身是:
SELECT
object_id AS ObjectID,
index_id AS IndexID,
avg_fragmentation_in_percent AS PercentFragment,
fragment_count AS TotalFrags,
avg_fragment_size_in_pages AS PagesPerFrag,
page_count AS NumPages
FROM
sys.dm_db_index_physical_stats(DB_ID('Hemasphere_Train'),NULL, NULL, NULL ,'DETAILED')
WHERE
avg_fragmentation_in_percent > 0
ORDER BY
ObjectID, IndexID
是否有可能获得表名称&索引名称而不是ID?如果是这样,我应该选择什么呢?
答案 0 :(得分:2)
对于表格,请使用object_name功能。
对于索引,请加入sys.indexes。
SELECT
ips.object_id AS ObjectID,
object_name(ips.object_id) as table_name,
ips.index_id AS IndexID,
i.name as index_name,
avg_fragmentation_in_percent AS PercentFragment,
fragment_count AS TotalFrags,
avg_fragment_size_in_pages AS PagesPerFrag,
page_count AS NumPages
FROM
sys.dm_db_index_physical_stats(DB_ID('Hemasphere_Train'),NULL, NULL, NULL ,'DETAILED') ips
inner join sys.indexes i
on ips.index_id = i.index_id
and ips.object_id = i.object_id
WHERE
avg_fragmentation_in_percent > 0
ORDER BY
ObjectID, IndexID