首页 > 数据库 > MySQL > 正文

迅速定位不合理的索引――MySQL索引调优

2022-08-03 16:45:42
字体:
来源:转载
供稿:网友
      __biz=MjM5MjIxNDA4NA==&mid=401131835&idx=1&sn=37c5fd9d3d8670fb379a1e0565e50eeb&scene=0#wechat_redirect
创建索引是门技术活,开发DBA的工作之一就是配合应用创建最优的索引。然大部分公司并没有开发DBA一职,大多数的索引创建需要由程序开发人员自己完成,这导致的一个后果是,索引创建的好与坏大部分情况下需要看这个程序猿的气质。
 
      通常,Inside君通过下面这条SQL语句来检视创建的索引(同时喝着咖啡,听着音乐),大部分情况下可以定位出90%的索引创建不合理情况:
 
版本《=5.6
 
      查找未被使用的索引:
mysql> select OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME from performance_schema.table_io_waits_summary_by_index_usage where INDEX_NAME is not null  and COUNT_STAR=0 and OBJECT_SCHEMA='xdq' and OBJECT_NAME='order_reasons_dispute' order by OBJECT_SCHEMA,OBJECT_NAME;
+---------------+-----------------------+------------+
| OBJECT_SCHEMA | OBJECT_NAME           | INDEX_NAME |
+---------------+-----------------------+------------+
| xdq           | order_reasons_dispute | PRIMARY    |
| xdq           | order_reasons_dispute | s_uid      |
| xdq           | order_reasons_dispute | b_uid      |
| xdq           | order_reasons_dispute | c_time     |
| xdq           | order_reasons_dispute | r_time     |
+---------------+-----------------------+------------+
5 rows in set (0.15 sec)
 
 
版本=5.7
mysql> select * from sys.schema_redundant_indexes   冗余索引
mysql> select * from schema_unused_indexes ;      未使用索引  --详见mysql5.7 sys schema视图详解   
mysql> select * from statements_with_full_table_scans; 使用全表扫描的sql语句 等。

(编辑:错新网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表