暂无图片
查找未被使用的索引
我来答
分享
暂无图片 匿名用户
查找未被使用的索引

mysql如何查找某个库中没有被使用过的索引,就是该索引建立之后,就没有被使用过,

我来答
添加附件
收藏
分享
问题补充
1条回答
默认
最新
刘贵宾

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`

暂无图片 评论
暂无图片 有用 0
打赏 0
回答交流
Markdown


请输入正文
提交
相关推荐
mysql架构方案?
回答 1
如果说有那么只能是MGR。因为金融要求强一致。MGR对网络要求高,不过是乐观锁。但是是唯一官方给的强一致高可用方案。
MySQL里change buffer的作用?
回答 5
已采纳
A主键更新的时候也进行也会使用changebuffer提升速度Insert语句B唯一建更新的时候,是通过changebuffer提升效率C如数据变更mysql里都是通过changebuffer进行数据
是否有任何查询方式来显示在MySQL上执行的最后一个查询?
回答 1
已采纳
可以开启generallog,对其进行分析,可以找到在mysql上执行的最后一个查询
mysql获取满足条件的相邻数据?
回答 1
已采纳
那为何不in1111/1114/1115/1116/1119/1120这些?如果说你是特殊要求就是一个数据的前后,那么你分两个SQL,中间用union连接起来。
mysql数据库定时备份占用大量线程,导致全局锁表,有啥好的解决方法么
回答 6
在从库上备份
MySQL 8.0 版本报错:Error 1193: unkonwn system variable 'validate password policy
回答 3
validatepassword组件确认安装了吗?安装mysql&gt;INSTALLCOMPONENT'file://componentvalidatepassword';QueryOK,0rows
半同步下的一主一丛,如果从库关机,主库会有啥结果?
回答 2
需要看看半同步怎么配置的,有的是配置的可以自动降级成异步,对主库没啥影响,有的配置是不自动降级,这种情况下主库就一直等备库的恢复,整个主库就会夯。
mysql 查所有的表,需要标注此表是否已分区的sql
回答 3
informationschema.partitions表显示分区表的情况。informationschema.tables表显示所有表的情况。这两个视图可以结合下。
MySQL8.x中,如何查看索引的创建时间?
回答 2
如果开启了binlog,并且没有删除,可以尝试从binlog,解析出来对应的表的Altertableaddindex的语句,来判断对应的索引被创建的时间点。
mysql里用了函数AES_DECRYPT,如何解密一张大表然后再导入Oracle?
回答 1
MySQL里有AESENCRYPT()与AESDECRYPT()加解密对称函数。我理解通过解密函数解完在固定格式导出,导入到oracle就可以了。