Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all indexes with included columns(nonkeys)

Tags:

I had tried sp_helpindex but it shows the columns that have index not the included columns. Please tell me how to list all indexes with included columns(nonkeys)?

like image 694
Prakash Avatar asked Aug 11 '13 13:08

Prakash


People also ask

What is index with included columns?

Indexes with included columns provide the greatest benefit when covering the query. This means that the index includes all columns referenced by your query, as you can add columns with data types, number or size not allowed as index key columns.

How do I find the index of a column in SQL Server?

You can use the sp_helpindex to view all the indexes of one table. And for all the indexes, you can traverse sys. objects to get all the indexes for each table. Only problem with this is that it only includes the index key columns, not the included columns.

How can we get the list of all the indexes on a table?

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. STATISTICS WHERE TABLE_SCHEMA = 'your_schema'; Removing the where clause will show you all indexes in all schemas.

Can a clustered index have included columns?

As per my understanding the leaf level of the clustered index are the actual data pages itself, and it stores entire row include with the key column (on which index is created). By definition a clustered index includes all columns... so there are none left to include.


2 Answers

Try this T-SQL query against the catalog views:

SELECT      IndexName = i.Name,     ColName = c.Name FROM      sys.indexes i INNER JOIN      sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id INNER JOIN      sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id WHERE     ic.is_included_column = 1 ORDER BY      i.Name 

It basically checks all indexes (sys.indexes) and then links to their columns and checks to see which columns are designed as included columns (ic.is_included_column = 1) and then lists out all those indexes and all those columns.

like image 63
marc_s Avatar answered Nov 16 '22 11:11

marc_s


Copied from http://www.midnightdba.com/Jen/2009/12/get-index-included-column-info/

SELECT   OBJECT_NAME(i.[object_id]) TableName ,  i.[name] IndexName ,  c.[name] ColumnName ,  ic.is_included_column ,  i.index_id ,  i.type_desc ,  i.is_unique ,  i.data_space_id ,  i.ignore_dup_key ,  i.is_primary_key ,  i.is_unique_constraint FROM   sys.indexes i  JOIN sys.index_columns ic ON ic.object_id = i.object_id and i.index_id = ic.index_id  JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id ORDER BY   tableName ,  ic.index_id ,  ic.index_column_id 
like image 33
Aaron Sherman Avatar answered Nov 16 '22 10:11

Aaron Sherman