搜鞥都哦 发表于 2017-12-12 23:22:43

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          |>| TABLE       | a             | t3          |>| TABLE       | a             | t3          | NULL      |          0 |          0 |         0 |+-------------+---------------+-------------+-------------+------------+------------+-------------+3 rows in set (0.01 sec) mysql> explain select>ERROR 1046 (3D000): No database selectedmysql> explain select>+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+|>+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+|1 | SIMPLE      | t3    | ref|>+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+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          |>| TABLE       | a             | t3          |>| 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 namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> select>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          |>| TABLE       | a             | t3          |>| TABLE       | a             | t3          | NULL      |          0 |          0 |         0 |+-------------+---------------+-------------+-------------+------------+------------+-------------+3 rows in set (0.00 sec) mysql> select>|>+------+|   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          |>| TABLE       | a             | t3          |>| 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          |>| TABLE       | a             | t3          |>| 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          |>| TABLE       | a             | t3          |>| TABLE       | a             | t3          | NULL      |          0 |          0 |         0 |+-------------+---------------+-------------+-------------+------------+------------+-------------+3 rows in set (0.01 sec) 索引被使用之后, 对应的值会增加。
页: [1]
查看完整版本: mysql中查看索引是否被使用到