MySQL是关系性数据库中的一种,查询功能强,数据一致性高,数据安全性高,支持二级索引。但性能方面稍逊于非关系性数据库,特别是百万级别以上的数据,很容易出现查询慢的现象。这时候须要分析查询慢的缘由,通常状况下是程序员sql写的烂,或者是没有键索引,或者是索引失效等缘由致使的。mysql
这时候MySQL 提供的 EXPLAIN 命令就尤为重要, 它能够对 SELECT
语句进行分析, 并输出 SELECT
执行的详细信息, 以供开发人员针对性优化.程序员
并且就在查询语句前加上 Explain 就成:sql
EXPLAIN SELECT * FROM customer WHERE id < 100;复制代码
首先须要创建两个测试用表及数据:typescript
CREATE TABLE `customer` (
`id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL DEFAULT '',
`age` INT(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name_index` (`name`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
INSERT INTO customer (name, age) VALUES ('a', 1);
INSERT INTO customer (name, age) VALUES ('b', 2);
INSERT INTO customer (name, age) VALUES ('c', 3);
INSERT INTO customer (name, age) VALUES ('d', 4);
INSERT INTO customer (name, age) VALUES ('e', 5);
INSERT INTO customer (name, age) VALUES ('f', 6);
INSERT INTO customer (name, age) VALUES ('g', 7);
INSERT INTO customer (name, age) VALUES ('h', 8);
INSERT INTO customer (name, age) VALUES ('i', 9);复制代码
CREATE TABLE `orders` (
`id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` BIGINT(20) unsigned NOT NULL DEFAULT 0, `product_name` VARCHAR(50) NOT NULL DEFAULT '',
`productor` VARCHAR(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `user_product_detail_index` (`user_id`, `product_name`, `productor`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
INSERT INTO orders (user_id, product_name, productor) VALUES (1, 'p1', 'WHH');
INSERT INTO orders (user_id, product_name, productor) VALUES (1, 'p2', 'WL');
INSERT INTO orders (user_id, product_name, productor) VALUES (1, 'p1', 'DX');
INSERT INTO orders (user_id, product_name, productor) VALUES (2, 'p1', 'WHH');
INSERT INTO orders (user_id, product_name, productor) VALUES (2, 'p5', 'WL');
INSERT INTO orders (user_id, product_name, productor) VALUES (3, 'p3', 'MA');
INSERT INTO orders (user_id, product_name, productor) VALUES (4, 'p1', 'WHH');
INSERT INTO orders (user_id, product_name, productor) VALUES (6, 'p1', 'WHH');
INSERT INTO orders (user_id, product_name, productor) VALUES (9, 'p8', 'TE');复制代码
EXPLAIN 命令的输出内容大体以下:数据库
mysql> explain select * from customer where id = 1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)复制代码
id: SELECT 查询的标识符. 每一个 SELECT 都会自动分配一个惟一的标识符.安全
select_type: SELECT 查询的类型.bash
table: 查询的是哪一个表性能
partitions: 匹配的分区测试
type: join 类型优化
possible_keys: 这次查询中可能选用的索引
key: 这次查询中确切使用到的索引.
ref: 哪一个字段或常数与 key 一块儿被使用
rows: 显示此查询一共扫描了多少行. 这个是一个估计值.
filtered: 表示此查询条件所过滤的数据的百分比
extra: 额外的信息
接下来咱们来重点看一下比较重要的几个字段.
SIMPLE —— 简单的select 查询,查询中不包含子查询或者UNION
PRIMARY —— 查询中若包含任何复杂的子查询,最外层查询则被标记为primary
UNION —— 表示此查询是 UNION 的第二或随后的查询
DEPENDENT UNION —— UNION 中的第二个或后面的查询语句, 取决于外面的查询
UNION RESULT —— 从UNION表获取结果的select结果
DERIVED —— 在from列表中包含的子查询被标记为derived(衍生)MySQL会递归执行这些子查询,把结果放在临时表里。
SUBQUERY —— 在select或where 列表中包含了子查询
DEPENDENT SUBQUERY —— 子查询中的第一个 SELECT, 取决于外面的查询. 即子查询依赖于外层查询的结果.
最多见的查询类别应该是 SIMPLE
了, 好比当咱们的查询没有子查询, 也没有 UNION 查询时, 那么一般就是 SIMPLE
类型, 例如:
mysql> explain select * from customer where id = 2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)复制代码
若是咱们使用了 UNION 查询, 那么 EXPLAIN 输出 的结果相似以下:
mysql> EXPLAIN (SELECT * FROM customer WHERE id IN (1, 2, 3))
-> UNION
-> (SELECT * FROM customer WHERE id IN (3, 4, 5));
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| 1 | PRIMARY | customer | NULL | range | PRIMARY | PRIMARY | 8 | NULL | 3 | 100.00 | Using where |
| 2 | UNION | customer | NULL | range | PRIMARY | PRIMARY | 8 | NULL | 3 | 100.00 | Using where |
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
3 rows in set, 1 warning (0.00 sec)复制代码
表示查询涉及的表或衍生表
type
字段比较重要, 它提供了判断查询是否高效的重要依据依据. 经过 type
字段, 咱们判断这次查询是 全表扫描
仍是 索引扫描
等.
type 经常使用的取值有:
system
: 表中只有一条数据. 这个类型是特殊的 const
类型.
const
: 针对主键或惟一索引的等值查询扫描, 最多只返回一行数据. const 查询速度很是快, 由于它仅仅读取一次便可.
例以下面的这个查询, 它使用了主键索引, 所以 type
就是 const
类型的.
mysql> explain select * from customer where id = 2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)复制代码
eq_ref
: 此类型一般出如今多表的 join 查询, 表示对于前表的每个结果, 都只能匹配到后表的一行结果. 而且查询的比较操做一般是 =
, 查询效率较高. 例如:
mysql> EXPLAIN SELECT * FROM customer, order_info WHERE customer.id = order_info.user_id\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: index
possible_keys: user_product_detail_index
key: user_product_detail_index
key_len: 314
ref: NULL
rows: 9
filtered: 100.00
Extra: Using where; Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: test.order_info.user_id
rows: 1
filtered: 100.00
Extra: NULL
2 rows in set, 1 warning (0.00 sec)复制代码
ref
: 此类型一般出如今多表的 join 查询, 针对于非惟一或非主键索引, 或者是使用了 最左前缀
规则索引的查询.
例以下面这个例子中, 就使用到了 ref
类型的查询:
mysql> EXPLAIN SELECT * FROM customer, order_info WHERE customer.id = order_info.user_id AND order_info.user_id = 5\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const
rows: 1
filtered: 100.00
Extra: NULL
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: ref
possible_keys: user_product_detail_index
key: user_product_detail_index
key_len: 9
ref: const
rows: 1
filtered: 100.00
Extra: Using index
2 rows in set, 1 warning (0.01 sec)复制代码
range
: 表示使用索引范围查询, 经过索引字段范围获取表中部分数据记录. 这个类型一般出如今 =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, IN() 操做中.
当 type
是 range
时, 那么 EXPLAIN 输出的 ref
字段为 NULL, 而且 key_len
字段是这次查询中使用到的索引的最长的那个.
例以下面的例子就是一个范围查询:
mysql> EXPLAIN SELECT * FROM customer WHERE id BETWEEN 2 AND 8 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: range
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: NULL
rows: 7
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)复制代码
index
: 表示全索引扫描(full index scan), 和 ALL 类型相似, 只不过 ALL 类型是全表扫描, 而 index 类型则仅仅扫描全部的索引, 而不扫描数据.index
类型一般出如今: 所要查询的数据直接在索引树中就能够获取到, 而不须要扫描数据. 当是这种状况时, Extra 字段 会显示 Using index
.
例如:
mysql> EXPLAIN SELECT name FROM customer \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: index
possible_keys: NULL
key: name_index
key_len: 152
ref: NULL
rows: 10
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)复制代码
上面的例子中, 咱们查询的 name 字段刚好是一个索引, 所以咱们直接从索引中获取数据就能够知足查询的需求了, 而不须要查询表中的数据. 所以这样的状况下, type 的值是 index
, 而且 Extra 的值是 Using index
.
ALL: 表示全表扫描, 这个类型的查询是性能最差的查询之一. 一般来讲, 咱们的查询不该该出现 ALL 类型的查询, 由于这样的查询在数据量大的状况下, 对数据库的性能是巨大的灾难. 如一个查询是 ALL 类型查询, 那么通常来讲能够对相应的字段添加索引来避免.
下面是一个全表扫描的例子, 能够看到, 在全表扫描时, possible_keys 和 key 字段都是 NULL, 表示没有使用到索引, 而且 rows 十分巨大, 所以整个查询效率是十分低下的.
mysql> EXPLAIN SELECT age FROM customer WHERE age = 20 \G*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: customer
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 10
filtered: 10.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)复制代码
一般来讲, 不一样的 type 类型的性能关系以下:ALL < index < range ~ index_merge < ref < eq_ref < const < system
ALL
类型由于是全表扫描, 所以在相同的查询条件下, 它是速度最慢的.
而 index
类型的查询虽然不是全表扫描, 可是它扫描了全部的索引, 所以比 ALL 类型的稍快.
后面的几种类型都是利用了索引来查询数据, 所以能够过滤部分或大部分数据, 所以查询效率就比较高了.
对程序员来讲,若保证查询至少达到range
级别或者最好能达到ref
则算是一个优秀而又负责的程序员。
possible_keys
表示 MySQL 在查询时, 可以使用到的索引. 注意, 即便有些索引在 possible_keys
中出现, 可是并不表示此索引会真正地被 MySQL 使用到. MySQL 在查询时具体使用了哪些索引, 由 key
字段决定.
此字段是 MySQL 在当前查询时所真正使用到的索引.
表示查询优化器使用了索引的字节数. 这个字段能够评估组合索引是否彻底被使用, 或只有最左部分字段被使用到.
key_len 的计算规则以下:
字符串
char(n): n 字节长度
varchar(n): 若是是 utf8 编码, 则是 3
数值类型:
TINYINT: 1字节
SMALLINT: 2字节
MEDIUMINT: 3字节
INT: 4字节
BIGINT: 8字节
时间类型
DATE: 3字节
TIMESTAMP: 4字节
DATETIME: 8字节
字段属性: NULL 属性 占用一个字节. 若是一个字段是 NOT NULL 的, 则没有此属性.
咱们来举两个简单的栗子:
mysql> EXPLAIN SELECT * FROM order_info WHERE user_id < 3 AND product_name = 'p1' AND productor = 'WHH' \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: range
possible_keys: user_product_detail_index
key: user_product_detail_index
key_len: 9
ref: NULL
rows: 5
filtered: 11.11
Extra: Using where; Using index
1 row in set, 1 warning (0.00 sec)复制代码
上面的例子是从表 order_info 中查询指定的内容, 而咱们今后表的建表语句中能够知道, 表 order_info
有一个联合索引:
KEY `user_product_detail_index` (`user_id`, `product_name`, `productor`)复制代码
不过此查询语句 WHERE user_id < 3 AND product_name = 'p1' AND productor = 'WHH'
中, 由于先进行 user_id 的范围查询, 而根据 最左前缀匹配
原则, 当遇到范围查询时, 就中止索引的匹配, 所以实际上咱们使用到的索引的字段只有 user_id
, 所以在 EXPLAIN
中, 显示的 key_len 为 9. 由于 user_id 字段是 BIGINT, 占用 8 字节, 而 NULL 属性占用一个字节, 所以总共是 9 个字节. 若咱们将user_id 字段改成 BIGINT(20) NOT NULL DEFAULT '0'
, 则 key_length 应该是8.
上面由于 最左前缀匹配
原则, 咱们的查询仅仅使用到了联合索引的 user_id
字段, 所以效率不算高.
接下来咱们来看一下下一个例子:
mysql> EXPLAIN SELECT * FROM order_info WHERE user_id = 1 AND product_name = 'p1' \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: ref
possible_keys: user_product_detail_index
key: user_product_detail_index
key_len: 161
ref: const,const
rows: 2
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)复制代码
此次的查询中, 咱们没有使用到范围查询, key_len 的值为 161. 为何呢? 由于咱们的查询条件 WHERE user_id = 1 AND product_name = 'p1'
中, 仅仅使用到了联合索引中的前两个字段, 所以 keyLen(user_id) + keyLen(product_name) = 9 + 50 * 3 + 2 = 161
rows 也是一个重要的字段. MySQL 查询优化器根据统计信息, 估算 SQL 要查找到结果集须要扫描读取的数据行数.
这个值很是直观显示 SQL 的效率好坏, 原则上 rows 越少越好.
EXplain 中的不少额外的信息会在 Extra 字段显示, 常见的有如下几种内容:
Using filesort
当 Extra 中有 Using filesort
时, 表示 MySQL 需额外的排序操做, 不能经过索引顺序达到排序效果. 通常有 Using filesort
, 都建议优化去掉, 由于这样的查询 CPU 资源消耗大.
例以下面的例子:
mysql> EXPLAIN SELECT * FROM order_info ORDER BY product_name \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: index
possible_keys: NULL
key: user_product_detail_index
key_len: 253
ref: NULL
rows: 9
filtered: 100.00
Extra: Using index; Using filesort
1 row in set, 1 warning (0.00 sec)复制代码
咱们的索引是
KEY `user_product_detail_index` (`user_id`, `product_name`, `productor`)复制代码
可是上面的查询中根据 product_name
来排序, 所以不能使用索引进行优化, 进而会产生 Using filesort
.
若是咱们将排序依据改成 ORDER BY user_id, product_name
, 那么就不会出现 Using filesort
了. 例如:
mysql> EXPLAIN SELECT * FROM order_info ORDER BY user_id, product_name \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: order_info
partitions: NULL
type: index
possible_keys: NULL
key: user_product_detail_index
key_len: 253
ref: NULL
rows: 9
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)复制代码
Using index
"覆盖索引扫描", 表示查询在索引树中就可查找所需数据, 不用扫描表数据文件, 每每说明性能不错
Using temporary查询有使用临时表, 通常出现于排序, 分组和多表 join 的状况, 查询效率不高, 建议优化.