816
In order to optimize database performance, it’s important to monitor and maintain the indexes of SQL Server tables. With T-SQL, you can easily retrieve index information for tables, including the schema, table name, index name, index type, and more. This post will show you how to use T-SQL to retrieve index information for your SQL Server tables, allowing you to monitor and optimize your database’s performance.
T-Sql
---------------------------------------------------------------------------------
--Index Structure Information Script
---------------------------------------------------------------------------------
SELECT
s.name AS SchemaName,
t.name AS Table_Name,
ix.name AS Index_Name,
ix.type_desc AS Index_Type, -- Type of the index (e.g. clustered, nonclustered)
col.name AS Index_Column_Name, -- Name of the indexed column
ixc.is_included_column AS Is_Included_Column -- Indicates whether the column is included in the index
FROM sys.indexes ix
INNER JOIN sys.index_columns ixc
ON ix.object_id = ixc.object_id
AND ix.index_id = ixc.index_id
INNER JOIN sys.columns col
ON ix.object_id = col.object_id
AND ixc.column_id = col.column_id
INNER JOIN sys.tables t
ON ix.object_id = t.object_id
INNER JOIN sys.schemas s
ON t.schema_id = s.schema_id
--WHERE t.name in('TableNameHere')
ORDER BY SchemaName, Table_Name
/* The script above retrieves information about the indexes of SQL Server tables,
including the schema, table name, index name, index type, indexed column name,
and whether the column is included in the index. */
/* To use this script, simply copy and paste it into a new query window
in SQL Server Management Studio, then execute the script. */
Note
For further information on this post, kindly refer to the original source provided in the link below.
Source: sqlshack