1. 程式人生 > >mysql中查看索引是否被使用到

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

and 索引 沒有 object format mysq count perf type

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) 索引被使用之後, 對應的值會增加。

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