mysql中查看索引是否被使用到

http://blog.sina.com.cn/s/blog_5037eacb0102vm5c.html

官方MySQL中查看索引是否被使用到:

 
在percona版本或marida中可以通过 information_schea.index_statistics查看得到,
在官方版本中如何查看呢?
 
 
select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage;
应该可以通过上面的sql得到。 如果read,fetch的次数都为0的话,应该是没有被使用过的。
 
通过下面的例子,可以证实:
 
启动mysql:
mysql> show create table a.t3;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                         |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t3    | CREATE TABLE `t3` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(10) NOT NULL DEFAULT 'bb',
  KEY `idx_t3` (`id`),
  KEY `idx_t3_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          0 |          0 |           0 |
| TABLE       | a             | t3          | idx_t3_name |          0 |          0 |           0 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.01 sec)
 
mysql> explain select id from t3 where id=1;
ERROR 1046 (3D000): No database selected
mysql> explain select id from a.t3 where id=1;
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key    | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
|  1 | SIMPLE      | t3    | ref  | idx_t3        | idx_t3 | 5       | const |    1 | Using index |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.00 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          0 |          0 |           0 |
| TABLE       | a             | t3          | idx_t3_name |          0 |          0 |           0 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.01 sec)
 
mysql> use a;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select id from t3 where id=1;
Empty set (0.00 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          1 |          1 |           1 |
| TABLE       | a             | t3          | idx_t3_name |          0 |          0 |           0 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.00 sec)
 
mysql> select id from t3 where id=10;                                                                                               +------+
| id   |
+------+
|   10 |
|   10 |
+------+
2 rows in set (0.00 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          4 |          4 |           4 |
| TABLE       | a             | t3          | idx_t3_name |          0 |          0 |           0 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.01 sec)
 
mysql> select name from t3 where name='a';
Empty set (0.00 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          4 |          4 |           4 |
| TABLE       | a             | t3          | idx_t3_name |          1 |          1 |           1 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.01 sec)
 
mysql> select name from t3 where name='name1';
+-------+
| name  |
+-------+
| name1 |
+-------+
1 row in set (0.01 sec)
 
mysql> select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_FETCH from performance_schema.table_io_waits_summary_by_index_usage where object_name='t3';
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| object_type | object_schema | object_name | index_name  | count_star | count_read | COUNT_FETCH |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
| TABLE       | a             | t3          | idx_t3      |          4 |          4 |           4 |
| TABLE       | a             | t3          | idx_t3_name |          3 |          3 |           3 |
| TABLE       | a             | t3          | NULL        |          0 |          0 |           0 |
+-------------+---------------+-------------+-------------+------------+------------+-------------+
3 rows in set (0.01 sec)
 
索引被使用之后, 对应的值会增加。

转载于:https://www.cnblogs.com/Jasonleo/p/6828524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值