场景:从mysql查出所有索引信息,在polardb批量重建索引。
select
CONCAT(
'CREATE ', if ( NON_UNIQUE = 1,
case UPPER(INDEX_TYPE)
when 'FULLTEXT'
then 'FULLTEXT INDEX'
when 'SPATIAL'
then 'SPATIAL INDEX'
else CONCAT( 'INDEX ', INDEX_NAME, ' ON ',
UPPER(TABLE_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 = 'andy_oa'
and index_name != 'PRIMARY'
-- 指定表名
and TABLE_NAME = 'core_log'
group by
TABLE_NAME,
INDEX_NAME,
NON_UNIQUE,
INDEX_TYPE
order by
TABLE_NAME asc,
INDEX_NAME asc;