当对大表进行数据迁移时,往往索引是个大问题,不但用时较长,且容易造成锁表,

此时针对某些情况,就可以先备份索引信息,然后删除原表索引,数据迁移完成之后,再重建索引

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 = 'epc_audi'
GROUP BY INDEX_TYPE , NON_UNIQUE , TABLE_NAME , INDEX_NAME
ORDER BY TABLE_NAME ASC , INDEX_NAME ASC;

其中USING BTREE/USING HASH 分别是使用不同的算法进行索引