Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Index Name from IndexId in SQL Server 2005

I'm running the following query to list size and fragmentation of the indexes:

SELECT object_name(object_id, database_id) as objectname), index_id, *
FROM sys.dm_db_index_usage_stats

Is there an SQL function I can use to convert the index_id to the index name?

like image 709
DHornpout Avatar asked Jul 24 '09 17:07

DHornpout


People also ask

How do I find the index name in SQL Server?

sp_helpindex is a system stored procedure which lists the information of all the indexes on a table or view. This is the easiest method to find the indexes in a table. sp_helpindex returns the name of the index, description of the index and the name of the column on which the index was created.

How do I find an index name?

To see the index for a specific table use SHOW INDEX: SHOW INDEX FROM yourtable; To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA.


1 Answers

I found a function on this page that should help you out:

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; 
like image 183
Espo Avatar answered Nov 16 '22 01:11

Espo