Skip to content

Instantly share code, notes, and snippets.

@lewiswalsh
Last active May 27, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewiswalsh/eeff3f637f89159da3fe526938befdc3 to your computer and use it in GitHub Desktop.
Save lewiswalsh/eeff3f637f89159da3fe526938befdc3 to your computer and use it in GitHub Desktop.
Export indexes from a database #mysql
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