Bootstrap

检查并删除mysql中多余的索引

接手了一个无人管理的mysql数据库,发现有的表建了巨量索引(单表40个索引);

需要对整个数据库做一个索引使用情况的检测,并按照需求删除不用的索引,写了下面的sql用于排查和生成drop index的sql;

注:mysql版本5.7.31

SELECT
	TAB.TABLE_SCHEMA,
	TAB.TABLE_NAME,
	IND.INDEX_NAME,
    TAB.INDEX_TYPE,
	TAB.NUM AS INDEX_NUM, #单表中索引总数
	IND.COUNT_STAR AS INDEX_USAGE, #索引使用量
	IO.COUNT_STAR AS TABLE_IO_USAGE, #表的使用量
	IO.COUNT_FETCH AS TABLE_IO_FETCH,
	IO.COUNT_INSERT AS TABLE_IO_INSERT,
	IO.COUNT_UPDATE AS TABLE_IO_UPDATE,
	IO.COUNT_DELETE AS TABLE_IO_DELETE,
	CONCAT('ALTER TABLE ',TAB.TABLE_SCHEMA,'.',TAB.TABLE_NAME,' DROP INDEX ',IND.INDEX_NAME,';') AS DROP_SQL
FROM
	( SELECT TABLE_SCHEMA, TABLE_NAME, COUNT( DISTINCT INDEX_NAME ) AS NUM, INDEX_TYPE
        FROM information_schema.statistics 
        GROUP BY TABLE_SCHEMA, TABLE_NAME 
        HAVING NUM > 5 ) AS TAB
LEFT JOIN performance_schema.table_io_waits_summary_by_index_usage AS IND 
    ON TAB.TABLE_SCHEMA &

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;