How to see indexes for a database or table in MySQL?

MysqlIndexingDatabase Schema

Mysql Problem Overview


How do I see if my database has any indexes on it?

How about for a specific table?

Mysql Solutions


Solution 1 - Mysql

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.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';

Removing the where clause will show you all indexes in all schemas.

Solution 2 - Mysql

If you want to see all indexes across all databases all at once:

use information_schema;
SELECT * FROM statistics;

Solution 3 - Mysql

SHOW INDEX FROM mytable FROM mydb;

SHOW INDEX FROM mydb.mytable;

See documentation.

Solution 4 - Mysql

You could use this query to get the no of indexes as well as the index names of each table in specified database.

SELECT TABLE_NAME,
       COUNT(1) index_count,
       GROUP_CONCAT(DISTINCT(index_name) SEPARATOR ',\n ') indexes
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'mydb'
      AND INDEX_NAME != 'primary'
GROUP BY TABLE_NAME
ORDER BY COUNT(1) DESC;

Solution 5 - Mysql

to see indexes you have created use

SHOW INDEX from your_table_name;

to see all indexes on a table ( created by DB and you)

SHOW EXTENDED INDEX from your_table_name;

Solution 6 - Mysql

To get all indexed columns per index in one column in the sequence order.

SELECT table_name AS `Table`,
       index_name AS `Index`,
       GROUP_CONCAT(column_name ORDER BY seq_in_index) AS `Columns`
FROM information_schema.statistics
WHERE table_schema = 'sakila'
GROUP BY 1,2;

Ref: http://blog.9minutesnooze.com/mysql-information-schema-indexes/

Solution 7 - Mysql

I propose this query:

SELECT DISTINCT s.*
FROM INFORMATION_SCHEMA.STATISTICS s
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t 
    ON t.TABLE_SCHEMA = s.TABLE_SCHEMA 
       AND t.TABLE_NAME = s.TABLE_NAME
       AND s.INDEX_NAME = t.CONSTRAINT_NAME 
WHERE 0 = 0
      AND t.CONSTRAINT_NAME IS NULL
      AND s.TABLE_SCHEMA = 'YOUR_SCHEMA_SAMPLE';

You found all Index only index.

Regard.

Solution 8 - Mysql

Why not show create table myTable ?

Someone told me this but I didn't see anyone mention here, anything bad?

It's neat if you just want to take a glance at the indexes along with column infomations.

Solution 9 - Mysql

This works in my case for getting table name and column name in the corresponding table for indexed fields.

SELECT TABLE_NAME , COLUMN_NAME, COMMENT 
FROM information_schema.statistics
WHERE table_schema = 'database_name';

Solution 10 - Mysql

To check all disabled indexes on db

SELECT INDEX_SCHEMA, COLUMN_NAME, COMMENT 
FROM information_schema.statistics
WHERE table_schema = 'mydb'
AND COMMENT = 'disabled'

Solution 11 - Mysql

You can check your indexes in MySQL workbench.under the performance reports tabs you can see all used indexes and unused indexes on the system. or you can fire the query.

select * from sys.schema_index_statistics;
 

Solution 12 - Mysql

To query the index information of a table, you use the SHOW INDEXES statement as follows:

 SHOW INDEXES FROM table_name;

You can specify the database name if you are not connected to any database or you want to get the index information of a table in a different database:

SHOW INDEXES FROM table_name 
IN database_name;

The following query is similar to the one above:

SHOW INDEXES FROM database_name.table_name;

Note that INDEX and KEYS are the synonyms of the INDEXES, IN is the synonym of the FROM, therefore, you can use these synonyms in the SHOW INDEXES column instead. For example:

SHOW INDEX IN table_name 
FROM database_name;

Or

 SHOW KEYS FROM tablename
 IN databasename;

Solution 13 - Mysql

we can directly see the indexes on to the table if we know the index name with below :

select * from all_indexes where index_name= 'your index'

Solution 14 - Mysql

select
	table_name,
    index_name,
    seq_in_index,
    column_name,
    non_unique,
    index_type,
    comment
from
	information_schema.statistics
where 1=1
	and table_schema = 'my_schema'
    and table_name = 'my_table'
order by 1,2,3,4,5,6

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - MysqlMark ByersView Answer on Stackoverflow
Solution 2 - MysqlRolandoMySQLDBAView Answer on Stackoverflow
Solution 3 - MysqlLiorKView Answer on Stackoverflow
Solution 4 - MysqladeviloperView Answer on Stackoverflow
Solution 5 - Mysqluser16304329View Answer on Stackoverflow
Solution 6 - MysqlJanakView Answer on Stackoverflow
Solution 7 - MysqlThierryView Answer on Stackoverflow
Solution 8 - MysqlRickView Answer on Stackoverflow
Solution 9 - MysqlasimView Answer on Stackoverflow
Solution 10 - MysqlDigital87View Answer on Stackoverflow
Solution 11 - MysqlGanesh GiriView Answer on Stackoverflow
Solution 12 - MysqlDilshan DilipView Answer on Stackoverflow
Solution 13 - Mysqluser15292302View Answer on Stackoverflow
Solution 14 - MysqlJohnView Answer on Stackoverflow