Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all indexes

Tags:

I'm wondering what the simplest way to list all indexes for all tables in a database is.

Should I call sp_helpindex for each table and store the results in a temp table, or is there an easier way?

Can anyone explain why constraints are stored in sysobjects but indexes are not?

like image 315
l15a Avatar asked Jan 08 '09 19:01

l15a


People also ask

How do I list all indexes in SQL?

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.

How do I show all indexes in Elasticsearch?

You can query localhost:9200/_status and that will give you a list of indices and information about each.


1 Answers

Here's an example of the kind of query you need:

select      i.name as IndexName,      o.name as TableName,      ic.key_ordinal as ColumnOrder,     ic.is_included_column as IsIncluded,      co.[name] as ColumnName from sys.indexes i  join sys.objects o on i.object_id = o.object_id join sys.index_columns ic on ic.object_id = i.object_id      and ic.index_id = i.index_id join sys.columns co on co.object_id = i.object_id      and co.column_id = ic.column_id where i.[type] = 2  and i.is_unique = 0  and i.is_primary_key = 0 and o.[type] = 'U' --and ic.is_included_column = 0 order by o.[name], i.[name], ic.is_included_column, ic.key_ordinal ; 

This one is somewhat specific to a certain purpose (I use it in a little C# app to find duplicate indexes and format the output so it's actually readable by a human). But you could easily adapt it to your needs.

like image 137
Eric Z Beard Avatar answered Sep 20 '22 16:09

Eric Z Beard