Last active
May 27, 2022 18:50
-
-
Save lewiswalsh/eeff3f637f89159da3fe526938befdc3 to your computer and use it in GitHub Desktop.
Export indexes from a database #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 Concat('ALTER TABLE ', table_name, ' ', 'ADD ', | |
IF(non_unique = 1, CASE Upper(index_type) | |
WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX' | |
WHEN 'SPATIAL' THEN 'SPATIAL INDEX' | |
ELSE Concat('INDEX ', index_name, ' USING ', | |
index_type) | |
end, IF(Upper(index_name) = 'PRIMARY', Concat( | |
'PRIMARY KEY USING ', index_type), | |
Concat('UNIQUE INDEX ', | |
index_name, ' USING ', | |
index_type))), '(', | |
Group_concat(DISTINCT Concat('', column_name, '') ORDER BY | |
seq_in_index | |
ASC | |
SEPARATOR ', '), ');') AS 'Show_Add_Indexes' | |
FROM information_schema.statistics | |
WHERE table_schema = '<DATABASE NAME>' | |
GROUP BY table_name, | |
index_name | |
ORDER BY table_name ASC, | |
index_name ASC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment