简体   繁体   中英

MySQL Select statement not returning tables with zero indexes

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM