2009-07-24 78 views

回答

22

我发现this页的功能应该帮助你:

CREATE FUNCTION dbo.index_name (@object_id int, @index_id int) 
RETURNS sysname 
AS 
BEGIN 
    RETURN(SELECT name FROM sys.indexes WHERE object_id = @object_id and index_id = @index_id) 
END; 
+8

或者,只需将此逻辑添加为原始查询 – 2009-07-24 17:55:51

+6

请注意,sys.indexes i s数据库特定。在上面的问题中,用户向sys.dm_db_index_usage_stats显示一个查询,并在OBJECT_NAME函数中指定database_id。这个函数只会返回当前打开的数据库的结果。 – 2014-05-21 21:10:22

10

或者更好的是:

SELECT OBJECT_NAME(d.object_id, d.database_id) AS objectname , 
     d.index_id , 
     i.name , 
     * 
FROM sys.dm_db_index_usage_stats AS d 
     LEFT OUTER JOIN sys.indexes AS i ON i.object_id = d.object_id 
              AND i.index_id = d.index_id 
-1

我来到这个解决方案,工程巨大:

create table dbo.IndexNames 
(database_id int not null, object_id int not null, index_id int not null, index_name sysname not null) 
go 

create procedure dbo.GatherIndexNames 
as 
begin  
declare @cur cursor 
,@name varchar(128) 
,@sql varchar(max) 

truncate table dbo.IndexNames 

set @cur = cursor for select name from sys.databases 
    where database_id >= 5 

open @cur 
fetch next from @cur into @name 
while @@fetch_status = 0 
begin 
    set @sql = ' 
    insert into dbo.IndexNames 
    (database_id, object_id, index_id, index_name) 
     select db_id(''' + @name + '''),t.object_id, i.index_id, i.name 
     from [' + @name + '].[sys].[tables] t 
     inner join [' + @name + '].[sys].[indexes] i 
     on t.OBJECT_ID = i.object_id 
     where i.index_id <> 0' 
    exec (@sql) 

    fetch next from @cur into @name 
end