-
-
Save DarkGL/e8615ae646725e4034b108399ed17170 to your computer and use it in GitHub Desktop.
Spotting missing indexes for MariaDB & MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
t.TABLE_SCHEMA, | |
t.TABLE_NAME, | |
c.COLUMN_NAME, | |
IFNULL( | |
kcu.CONSTRAINT_NAME, 'Not indexed' | |
) AS `Index` | |
FROM | |
information_schema.TABLES t | |
INNER JOIN information_schema.`COLUMNS` c ON c.TABLE_SCHEMA = t.TABLE_SCHEMA | |
AND c.TABLE_NAME = t.TABLE_NAME | |
AND c.COLUMN_NAME LIKE '%Id' | |
LEFT JOIN information_schema.`KEY_COLUMN_USAGE` kcu ON kcu.TABLE_SCHEMA = t.TABLE_SCHEMA | |
AND kcu.TABLE_NAME = t.TABLE_NAME | |
AND kcu.COLUMN_NAME = c.COLUMN_NAME | |
AND kcu.ORDINAL_POSITION = 1 | |
WHERE | |
kcu.TABLE_SCHEMA IS NULL | |
AND t.TABLE_SCHEMA NOT IN ( | |
'information_schema', 'performance_schema', | |
'mysql' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment