MySQL查詢冗餘索引和未使用過的索引

2021-10-18 17:18:19 字數 3786 閱讀 5434

mysql5.7及以上版本提供直接查詢冗餘索引、重複索引和未使用過索引的檢視,直接查詢即可。

查詢冗餘索引、重複索引

select * from sys.schema_redundant_indexes;
查詢未使用過的索引

select * from sys.schema_unused_indexes;
如果想在5.6和5.5版本使用,將檢視轉換成sql語句查詢即可

查詢冗餘索引、重複索引

select a.`table_schema`,a.`table_name`,a.`index_name`,a.`index_columns`,b.`index_name`,b.`index_columns`,concat('alter table `',a.`table_schema`,'`.`',a.`table_name`,'` drop index `',a.`index_name`,'`') from ((select `information_schema`.`statistics`.`table_schema` as `table_schema`,`information_schema`.`statistics`.`table_name` as `table_name`,`information_schema`.`statistics`.`index_name` as `index_name`,max(`information_schema`.`statistics`.`non_unique`) as `non_unique`,max(if(isnull(`information_schema`.`statistics`.`sub_part`),0,1)) as `subpart_exists`,group_concat(`information_schema`.`statistics`.`column_name` order by `information_schema`.`statistics`.`seq_in_index` asc separator ',') as `index_columns` from `information_schema`.`statistics` where ((`information_schema`.`statistics`.`index_type` = 'btree') and (`information_schema`.`statistics`.`table_schema` not in ('mysql','sys','information_schema','performance_schema'))) group by `information_schema`.`statistics`.`table_schema`,`information_schema`.`statistics`.`table_name`,`information_schema`.`statistics`.`index_name`) a join (select `information_schema`.`statistics`.`table_schema` as `table_schema`,`information_schema`.`statistics`.`table_name` as `table_name`,`information_schema`.`statistics`.`index_name` as `index_name`,max(`information_schema`.`statistics`.`non_unique`) as `non_unique`,max(if(isnull(`information_schema`.`statistics`.`sub_part`),0,1)) as `subpart_exists`,group_concat(`information_schema`.`statistics`.`column_name` order by `information_schema`.`statistics`.`seq_in_index` asc separator ',') as `index_columns` from `information_schema`.`statistics` where ((`information_schema`.`statistics`.`index_type` = 'btree') and (`information_schema`.`statistics`.`table_schema` not in ('mysql','sys','information_schema','performance_schema'))) group by `information_schema`.`statistics`.`table_schema`,`information_schema`.`statistics`.`table_name`,`information_schema`.`statistics`.`index_name`) b on(((a.`table_schema` = b.`table_schema`) and (a.`table_name` = b.`table_name`)))) where ((a.`index_name` <> b.`index_name`) and (((a.`index_columns` = b.`index_columns`) and ((a.`non_unique` > b.`non_unique`) or ((a.`non_unique` = b.`non_unique`) and (if((a.`index_name` = 'primary'),'',a.`index_name`) > if((b.`index_name` = 'primary'),'',b.`index_name`))))) or ((locate(concat(a.`index_columns`,','),b.`index_columns`) = 1) and (a.`non_unique` = 1)) or ((locate(concat(b.`index_columns`,','),a.`index_columns`) = 1) and (b.`non_unique` = 0))));
查詢未使用過的索引

select `information_schema`.`statistics`.`table_schema` as `table_schema`,`information_schema`.`statistics`.`table_name` as `table_name`,`information_schema`.`statistics`.`index_name` as `index_name`,max(`information_schema`.`statistics`.`non_unique`) as `non_unique`,max(if(isnull(`information_schema`.`statistics`.`sub_part`),0,1)) as `subpart_exists`,group_concat(`information_schema`.`statistics`.`column_name` order by `information_schema`.`statistics`.`seq_in_index` asc separator ',') as `index_columns` from `information_schema`.`statistics` where ((`information_schema`.`statistics`.`index_type` = 'btree') and (`information_schema`.`statistics`.`table_schema` not in ('mysql','sys','information_schema','performance_schema'))) group by `information_schema`.`statistics`.`table_schema`,`information_schema`.`statistics`.`table_name`,`information_schema`.`statistics`.`index_name`

mysql 查詢冗餘索引 冗餘索引對查詢效率的影響

結果一 與執行計畫相關的索引 出現在possible keys的那些 索引的數量與sql執行消耗時間成正比。create index idx1 on test index performance col1 create index idx2 on test index performance col...

查詢Mysql未使用的索引

在mysql中如何找出未使用或使用次數很少的索引,這樣文章比較多,但很少文章提到用這些方法存在的風險。這篇文章主要記錄,我對如何找未使用索引的理解及風險 目前還未找到理想方法 能像oracle儲存執行計畫,根據執行計畫 v sql plan 來判斷索引使用情況是比較安全。當然oracle的index...

mysql冗餘索引 MySQL 冗餘和重複索引

冗餘和重複索引冗餘和重複索引的概念 mysql允許在相同列上建立多個索引,無論是有.冗餘和重複索引 冗餘和重複索引的概念 mysql允許在相同列上建立多個索引,無論是有意的還是無意的。mysql需要單獨維護重複的索引,香港虛擬主機,並且優化器在優化查詢的時候也需要逐個地進行考慮,香港虛擬主機,這會影...