How do I see if my database has any indexes on it?
How about for a specific table?
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.
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;
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.
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/
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;