0.在sys庫中查看沒用的索引mysql
root@localhost [sys]>select * from schema_unused_indexes; +-------------------+-------------+------------+ | object_schema | object_name | index_name | +-------------------+-------------+------------+ | sysbench_testdata | sbtest1 | k_1 | | sysbench_testdata | sbtest10 | k_10 | | sysbench_testdata | sbtest3 | k_3 | | sysbench_testdata | sbtest4 | k_4 | | sysbench_testdata | sbtest5 | k_5 | | sysbench_testdata | sbtest6 | k_6 | | sysbench_testdata | sbtest7 | k_7 | | sysbench_testdata | sbtest8 | k_8 | | sysbench_testdata | sbtest9 | k_9 | +-------------------+-------------+------------+ 9 rows in set (0.00 sec)
root@localhost [sysbench_testdata]>show status like '%Handler_read%'; +-----------------------+---------+ | Variable_name | Value | +-----------------------+---------+ | Handler_read_first | 7 | | Handler_read_key | 29 | | Handler_read_last | 0 | | Handler_read_next | 8446377 | | Handler_read_prev | 0 | | Handler_read_rnd | 20 | | Handler_read_rnd_next | 8344612 | +-----------------------+---------+ 7 rows in set (0.00 sec)
Handler_read_key這個值表明了一個行將索引值讀的次數,很低的值代表增長索引獲得的性能改善不高,由於索引並不常常使用。sql
Handler_read_rnd_next 的值高則查詢低效,而且應該創建索引補救。這個值是指在數據文件中讀下一行的請求數。若是正進行大量的表掃描,Handler_read_rnd_next的值較高,則一般說明表索引不正確或查詢沒有利用索引性能
2.查看具體某一個sql的索引使用狀況 :code
root@localhost [sysbench_testdata]>explain select k from sbtest2 where k=432 limit 2; +----+-------------+---------+------------+------+---------------+------+---------+-------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------+------------+------+---------------+------+---------+-------+--------+----------+-------------+ | 1 | SIMPLE | sbtest2 | NULL | ref | k_2 | k_2 | 4 | const | 110944 | 100.00 | Using index | +----+-------------+---------+------------+------+---------------+------+---------+-------+--------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
字段說明:
Type:告訴咱們對錶所使用的訪問方式,主要包含以下集中類型;
◇ all:全表掃描
◇ const:讀常量,且最多隻會有一條記錄匹配,因爲是常量,因此實際上只須要讀一次;
◇ eq_ref:最多隻會有一條匹配結果,通常是經過主鍵或者惟一鍵索引來訪問;
◇ fulltext:
◇ index:全索引掃描;
◇ index_merge:查詢中同時使用兩個(或更多)索引,而後對索引結果進行merge 以後再讀取表數據;
◇ index_subquery:子查詢中的返回結果字段組合是一個索引(或索引組合),但不是一個主鍵或者惟一索引;
◇ rang:索引範圍掃描;
◇ ref:Join 語句中被驅動表索引引用查詢;
◇ ref_or_null:與ref 的惟一區別就是在使用索引引用查詢以外再增長一個空值的查詢;
◇ system:系統表,表中只有一行數據;
◇ unique_subquery:子查詢中的返回結果字段組合是主鍵或者惟一約束;orm
possible_keys:可能能夠利用的索引的名字。這裏的索引名字是建立索引時指定的索引暱稱;若是索引沒有暱稱,則默認顯示的是索引中第一個列的名字(在本例中,它是「firstname」)。默認索引名字的含義每每不是很明顯。 server
key:它顯示了MySQL實際使用的索引的名字。若是它爲空(或NULL),則MySQL不使用索引。 索引
key_len:索引中被使用部分的長度,以字節計it
ref:列出是經過常量(const),仍是某個表的某個字段(若是是join)來過濾(經過key)
的;io
rows:MySQL所認爲的它在找到正確的結果以前必須掃描的記錄數。顯然,這裏最理想的數字就是1。table
3.經過performance_schema能夠查詢到.查看sbtest2表索引狀況的查詢語句:
root@localhost [sysbench_testdata]>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='sbtest2';
root@localhost [sysbench_testdata]>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='sbtest2'; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 1697669 Current database: sysbench_testdata +-------------+-------------------+-------------+------------+------------+------------+-------------+ | object_type | object_schema | object_name | index_name | count_star | count_read | COUNT_FETCH | +-------------+-------------------+-------------+------------+------------+------------+-------------+ | TABLE | sysbench_testdata | sbtest2 | PRIMARY | 0 | 0 | 0 | | TABLE | sysbench_testdata | sbtest2 | k_2 | 76287298 | 76287298 | 76287298 | | TABLE | sysbench_testdata | sbtest2 | NULL | 8344631 | 8344631 | 8344631 | +-------------+-------------------+-------------+------------+------------+------------+-------------+ 3 rows in set (0.00 sec) root@localhost [sysbench_testdata]>select k from sbtest2 where k=432 limit 2; +-----+ | k | +-----+ | 432 | | 432 | +-----+ 2 rows in set (0.00 sec) root@localhost [sysbench_testdata]>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='sbtest2'; +-------------+-------------------+-------------+------------+------------+------------+-------------+ | object_type | object_schema | object_name | index_name | count_star | count_read | COUNT_FETCH | +-------------+-------------------+-------------+------------+------------+------------+-------------+ | TABLE | sysbench_testdata | sbtest2 | PRIMARY | 0 | 0 | 0 | | TABLE | sysbench_testdata | sbtest2 | k_2 | 76287300 | 76287300 | 76287300 | | TABLE | sysbench_testdata | sbtest2 | NULL | 8344631 | 8344631 | 8344631 | +-------------+-------------------+-------------+------------+------------+------------+-------------+ 3 rows in set (0.01 sec) root@localhost [sysbench_testdata]>select k from sbtest2 where id=432 limit 2; +-------+ | k | +-------+ | 49866 | +-------+ 1 row in set (0.00 sec) root@localhost [sysbench_testdata]>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='sbtest2'; +-------------+-------------------+-------------+------------+------------+------------+-------------+ | object_type | object_schema | object_name | index_name | count_star | count_read | COUNT_FETCH | +-------------+-------------------+-------------+------------+------------+------------+-------------+ | TABLE | sysbench_testdata | sbtest2 | PRIMARY | 1 | 1 | 1 | | TABLE | sysbench_testdata | sbtest2 | k_2 | 76287300 | 76287300 | 76287300 | | TABLE | sysbench_testdata | sbtest2 | NULL | 8344631 | 8344631 | 8344631 | +-------------+-------------------+-------------+------------+------------+------------+-------------+ 3 rows in set (0.00 sec) root@localhost [sysbench_testdata]>