在MySQL里,我們一般使用SHOW STATUS查詢服務器狀態,語法一般來說如下:
SHOW [GLOBAL | SESSION] STATUS [LIKE ‘pattern’ | WHERE expr]
執行命令后會看到很多內容,其中有一部分是Handler_read_*,它們顯示了數據庫處理SELECT查詢語句的狀態,對於調試SQL語句有很大意義,可惜實際很多人並不理解它們的實際意義,本文簡單介紹一下:
為了讓介紹更易懂,先建立一個測試用的表:
CREATE TABLE `foo` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `col1` varchar(10) NOT NULL, `col2` text NOT NULL, PRIMARY KEY (`id`), KEY `col1` (`col1`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=gbk
mysql> INSERT INTO `foo` (`id`, `col1`, `col2`) VALUES (1, "a", "a"), (2, "b", "b"), (3, "c", "c"), (4, "d", "d"), (5, "e", "e"), (6, "f", "f"), (7, "g", "g"), (8, "h", "h"), (9, "i", "i");
在下面的測試里,每次執行SQL時按照如下過程執行:
FLUSH STATUS;
SELECT …;
SHOW SESSION STATUS LIKE ‘Handler_read%’;
EXPLAIN SELECT …;
Handler_read_first
The number of times the first entry was read from an index. If this value is high, it suggests that the server is doing a lot of full index scans; for example, SELECT col1 FROM foo, assuming that col1 is indexed.
此選項表明SQL是在做一個全索引掃描,注意是全部,而不是部分,所以說如果存在WHERE語句,這個選項是不會變的。如果這個選項的數值很大,既是好事 也是壞事。說它好是因為畢竟查詢是在索引里完成的,而不是數據文件里,說它壞是因為大數據量時,簡便是索引文件,做一次完整的掃描也是很費時的。
FLUSH STATUS; SELECT col1 FROM foo;
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 1 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 9 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.03 sec)
mysql> EXPLAIN SELECT col1 FROM foo\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: foo type: index possible_keys: NULL key: col1 key_len: 22 ref: NULL rows: 9 Extra: Using index 1 row in set (0.01 sec)
Handler_read_key
The number of requests to read a row based on a key. If this value is high, it is a good indication that your tables are properly indexed for your queries.
此選項數值如果很高,那么恭喜你,你的系統高效的使用了索引,一切運轉良好。
FLUSH STATUS;
mysql> SELECT * FROM foo WHERE col1="e"; +----+------+------+ | id | col1 | col2 | +----+------+------+ | 5 | e | e | +----+------+------+ 1 row in set (0.02 sec)
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 1 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.02 sec)
mysql> explain SELECT * FROM foo WHERE col1="e"; +----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------+ | 1 | SIMPLE | foo | ref | col1 | col1 | 22 | const | 1 | Using index condition | +----+-------------+-------+------+---------------+------+---------+-------+------+-----------------------+ 1 row in set (0.01 sec)
Handler_read_next
The number of requests to read the next row in key order. This value is incremented if you are querying an index column with a range constraint or if you are doing an index scan.
此選項表明在進行索引掃描時,按照索引從數據文件里取數據的次數。
mysql> FLUSH STATUS; Query OK, 0 rows affected (0.00 sec) mysql> SELECT col1 FROM foo ORDER BY col1 ASC; +------+ | col1 | +------+ | a | | b | | c | | d | | e | | f | | g | | h | | i | +------+ 9 rows in set (0.01 sec)
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 1 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 9 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.01 sec)
mysql> explain SELECT col1 FROM foo ORDER BY col1 ASC\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: foo type: index possible_keys: NULL key: col1 key_len: 22 ref: NULL rows: 9 Extra: Using index 1 row in set (0.00 sec) ERROR: No query specified
Handler_read_prev
The number of requests to read the previous row in key order. This read method is mainly used to optimize ORDER BY … DESC.
此選項表明在進行索引掃描時,按照索引倒序從數據文件里取數據的次數,一般就是ORDER BY … DESC。
mysql> FLUSH STATUS; Query OK, 0 rows affected (0.00 sec) mysql> SELECT col1 FROM foo ORDER BY col1 DESC; +------+ | col1 | +------+ | i | | h | | g | | f | | e | | d | | c | | b | | a | +------+ 9 rows in set (0.01 sec)
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 1 | | Handler_read_next | 0 | | Handler_read_prev | 9 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | +-----------------------+-------+ 7 rows in set (0.03 sec)
mysql> explain SELECT col1 FROM foo ORDER BY col1 DESC; +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | foo | index | NULL | col1 | 22 | NULL | 9 | Using index | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 1 row in set (0.01 sec)
Handler_read_rnd
The number of requests to read a row based on a fixed position. This value is high if you are doing a lot of queries that require sorting of the result. You probably have a lot of queries that require MySQL to scan entire tables or you have joins that don’t use keys properly.
簡單的說,就是查詢直接操作了數據文件,很多時候表現為沒有使用索引或者文件排序。
mysql> FLUSH STATUS; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM foo ORDER BY col2 DESC; +----+------+------+ | id | col1 | col2 | +----+------+------+ | 9 | i | i | | 8 | h | h | | 7 | g | g | | 6 | f | f | | 5 | e | e | | 4 | d | d | | 3 | c | c | | 2 | b | b | | 1 | a | a | +----+------+------+ 9 rows in set (0.02 sec)
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 1 | | Handler_read_key | 10 | | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 9 | | Handler_read_rnd_next | 10 | +-----------------------+-------+ 7 rows in set (0.02 sec)
mysql> explain SELECT * FROM foo ORDER BY col2 DESC; +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | 1 | SIMPLE | foo | ALL | NULL | NULL | NULL | NULL | 9 | Using filesort | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.01 sec)
Handler_read_rnd_next
The number of requests to read the next row in the data file. This value is high if you are doing a lot of table scans. Generally this suggests that your tables are not properly indexed or that your queries are not written to take advantage of the indexes you have.
此選項表明在進行數據文件掃描時,從數據文件里取數據的次數。
mysql> FLUSH STATUS; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM foo; +----+------+------+ | id | col1 | col2 | +----+------+------+ | 1 | a | a | | 2 | b | b | | 3 | c | c | | 4 | d | d | | 5 | e | e | | 6 | f | f | | 7 | g | g | | 8 | h | h | | 9 | i | i | +----+------+------+ 9 rows in set (0.01 sec)
mysql> SHOW SESSION STATUS LIKE "%handler_read%"; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 1 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 10 | +-----------------------+-------+ 7 rows in set (0.02 sec)
mysql> EXPLAIN SELECT * FROM foo\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: foo
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 9
Extra: NULL
1 row in set (0.01 sec)
后記:不同平台,不同版本的MySQL,在運行上面例子的時候,Handler_read_*的數值可能會有所不同,這並不要緊,關鍵是你要意識到 Handler_read_*可以協助你理解MySQL處理查詢的過程,很多時候,為了完成一個查詢任務,我們往往可以寫出幾種查詢語句,這時,你不妨挨 個按照上面的方式執行,根據結果中的Handler_read_*數值,你就能相對容易的判斷各種查詢方式的優劣。
說到判斷查詢方式優劣這個問題,就再順便提提show profile語法,在新版MySQL里提供了這個功能:
mysql> set profiling=on; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show profile; +----------------+----------+ | Status | Duration | +----------------+----------+ | starting | 0.000840 | | query end | 0.000116 | | closing tables | 0.000097 | | freeing items | 0.000102 | | cleaning up | 0.000205 | +----------------+----------+ 5 rows in set, 1 warning (0.01 sec) mysql> show profiles; +----------+------------+-------------------+ | Query_ID | Duration | Query | +----------+------------+-------------------+ | 1 | 0.00135925 | show warnings | | 2 | 0.00127000 | show warnings | | 3 | 0.18649600 | SELECT * FROM foo | +----------+------------+-------------------+ 3 rows in set, 1 warning (0.00 sec)
http://www.path8.net/tn/archives/5613
參考鏈接:http://www.shinguz.ch/MySQL/mysql_handler_read_status.html
from http://hi.baidu.com/thinkinginlamp/blog/item/31690cd7c4bc5cdaa144df9c.html