MySQL Select statement not returning tables with zero indexes

testPanda1089 :

I am attempting to run this query in order to find out the number of indexes in each table. However, it doesn't seem to be returning any tables with zero indexes. (Running this on Mac Workbench)

SELECT TABLE_NAME,
   COUNT(1) index_count,
   GROUP_CONCAT(index_name SEPARATOR ',\n ') indexes
   FROM INFORMATION_SCHEMA.STATISTICS
   WHERE TABLE_SCHEMA = '<my_schema>'
   GROUP BY TABLE_NAME;

I have attempted to change the values inside count, to no avail. Ant tips or advice is appreciated!

Barmar :

The STATISTICS table only has rows for indexes; if a table doesn't have any indexes, it won't be in there, so just querying that table won't return anything.

You need to left join the TABLES table with STATISTICS so you can get all the tables.

SELECT t.table_name, COUNT(s.index_name) index_count, IFNULL(GROUP_CONCAT(s.index_name SEPARATOR ',\n '), '') indexes
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT JOIN INFORMATION_SCHEMA.STATISTICS AS s ON t.table_schema = s.table_schema AND t.table_name = s.table_name
WHERE t.table_schema = '<my_schema>'
GROUP BY t.table_name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25642&siteId=1