博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL之Handler_read_*
阅读量:3603 次
发布时间:2019-05-20

本文共 5387 字,大约阅读时间需要 17 分钟。

作者:老王

在MySQL里,我们一般使用SHOW STATUS查询服务器状态,语法一般来说如下:

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

执行命令后会看到很多内容,其中有一部分是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`)
);

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      | 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_*数值,你就能相对容易的判断各种查询方式的优劣。

参考链接:

转载地址:http://hfszn.baihongyu.com/

你可能感兴趣的文章
IDEA恢复布局
查看>>
重定向和请求转发的区别
查看>>
Map、Set、List集合区别(看完秒懂)
查看>>
普通用户使用docker命令遇到提示需要提升权限时的解决方法
查看>>
webpack打包技术
查看>>
Leecode 面试题09用两个栈实现队列
查看>>
fastdfs连接超时报错解决方案
查看>>
Leecode202. 快乐数
查看>>
windows10解决80端口被占用的问题
查看>>
ElasticSearch快速入门之创建索引库、创建映射、创建文档、搜索文档
查看>>
用故事巧妙帮助理解公钥和私钥的区别和联系
查看>>
application.properties 文件和 application.yml 文件区别以及加载顺序
查看>>
阿里云服务器安装docker,拉取常用的mysql,redis,nginx等镜像
查看>>
为什么timestamp到2038年就截止了?
查看>>
设计模式之适配器模式
查看>>
设计模式之工厂模式
查看>>
设计模式之原型模式
查看>>
设计模式之对象池模式
查看>>
设计模式之责任链模式 Java实例代码 + Tomcat责任链模式应用+安卓责任链模式应用
查看>>
设计模式之命令模式 Java实例讲解 + 线程池中的应用场景
查看>>