MySQL之Handler_read_*查看索引使用狀況

在MySQL裏,咱們通常使用SHOW STATUS查詢服務器狀態,語法通常來講以下:mysql

SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern' | WHERE expr]sql

執行命令後會看到不少內容,其中有一部分是Handler_read_*,它們顯示了數據庫處理SELECT查詢語句的狀態,對於調試SQL語句有很大意義,惋惜實際不少人並不理解它們的實際意義,本文簡單介紹一下:數據庫

爲了讓介紹更易懂,先創建一個測試用的表:服務器

CREATE TABLE IF NOT EXISTS `foo` (
`id` int(10) unsigned NOT NULL auto_increment,
`col1` varchar(10) NOT NULL,
`col2` text NOT NULL,
PRIMARY KEY (`id`),
KEY `col1` (`col1`)
);ide

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 ...;ui

Handler_read_firstthis

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 | 0 |
| Handler_read_next | 9 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+-----------------------+-------+
6 rows in set (0.00 sec)

mysql> EXPLAIN SELECT col1 FROM foo\G
type: index
Extra: Using index

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;

SELECT * FROM foo WHERE col1 = 'e';

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 1 |
| Handler_read_next | 1 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 'e'\G
type: ref
Extra: Using where


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.

此選項代表在進行索引掃描時,按照索引從數據文件裏取數據的次數。

FLUSH STATUS;

SELECT col1 FROM foo ORDER BY col1 ASC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 1 |
| Handler_read_key | 0 |
| Handler_read_next | 9 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo WHERE col1 = 'e'\G
type: index
Extra: Using index

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。

FLUSH STATUS;

SELECT col1 FROM foo ORDER BY col1 DESC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 9 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+-----------------------+-------+

mysql> EXPLAIN SELECT col1 FROM foo ORDER BY col1 DESC\G
type: index
Extra: Using index

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.

簡單的說,就是查詢直接操做了數據文件,不少時候表現爲沒有使用索引或者文件排序。

FLUSH STATUS;

SELECT * FROM foo ORDER BY col2 DESC;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 9 |
| Handler_read_rnd_next | 10 |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo ORDER BY col2 DESC\G
type: ALL
Extra: Using filesort

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.

此選項代表在進行數據文件掃描時,從數據文件裏取數據的次數。

FLUSH STATUS;

SELECT * FROM foo;

mysql> SHOW SESSION STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 10 |
+-----------------------+-------+

mysql> EXPLAIN SELECT * FROM foo ORDER BY col2 DESC\G
type: ALL
Extra: Using filesort


後記:不一樣平臺,不一樣版本的MySQL,在運行上面例子的時候,Handler_read_*的數值可能會有所不一樣,這並沒關係,關鍵是你要意識到Handler_read_*能夠協助你理解MySQL處理查詢的過程,不少時候,爲了完成一個查詢任務,咱們每每能夠寫出幾種查詢語句,這時,你不妨挨個按照上面的方式執行,根據結果中的Handler_read_*數值,你就能相對容易的判斷各類查詢方式的優劣。

說到判斷查詢方式優劣這個問題,就再順便提提show profile語法,在新版MySQL裏提供了這個功能:

mysql> set profiling=on;

mysql> use mysql;
mysql> select * from user;

mysql> show profile;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 0.000078 |
| Opening tables | 0.000022 |
| System lock | 0.000010 |
| Table lock | 0.000014 |
| init | 0.000054 |
| optimizing | 0.000008 |
| statistics | 0.000015 |
| preparing | 0.000014 |
| executing | 0.000007 |
| Sending data | 0.000139 |
| end | 0.000007 |
| query end | 0.000007 |
| freeing items | 0.000044 |
| logging slow query | 0.000004 |
| cleaning up | 0.000005 |
+--------------------+----------+
15 rows in set (0.00 sec)

mysql> show profiles;+----------+------------+--------------------+| Query_ID | Duration | Query |+----------+------------+--------------------+| 1 | 0.00017725 | SELECT DATABASE(). || 2 | 0.00042675 | select * from user |+----------+------------+--------------------+2 rows in set (0.00 sec)

相關文章
相關標籤/搜索