从查询中的Object_ID列出表名称

问题描述 投票:0回答:2

我发现这个脚本将使用系统函数

sys.dm_db_index_physical_stats
分析数据库的碎片,并为我提供给定数据库的 objectID、IndexID 和碎片百分比,但我想知道表名称和索引名称,而不是身份证号。

该脚本可在此处找到:对 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?如果是这样,我应该选择什么?

sql-server-2008 indexing
2个回答
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

0
投票

尝试添加数据库名称和架构所有者。

© www.soinside.com 2019 - 2024. All rights reserved.