《MYSQL数据库MySQL 性能优化神器Explain介绍及使用详情》要点:
本文介绍了MYSQL数据库MySQL 性能优化神器Explain介绍及使用详情,希望对您有用。如果有疑问,可以联系我们。
- EXPLAIN SELECT * from user_info WHERE id < 300;
MYSQL实例为了接下来方便演示 EXPLAIN 的使用, 首先我们需要建立两个测试用的表, 并添加相应的数据:
- CREATE TABLE `user_info` (
- `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
- `name` VARCHAR(50) NOT NULL DEFAULT '',
- `age` INT(11) DEFAULT NULL,
- PRIMARY KEY (`id`),
- KEY `name_index` (`name`)
- )
- ENGINE = InnoDB
- DEFAULT CHARSET = utf8
- INSERT INTO user_info (name, age) VALUES ('xys', 20);
- INSERT INTO user_info (name, age) VALUES ('a', 21);
- INSERT INTO user_info (name, age) VALUES ('b', 23);
- INSERT INTO user_info (name, age) VALUES ('c', 50);
- INSERT INTO user_info (name, age) VALUES ('d', 15);
- INSERT INTO user_info (name, age) VALUES ('e', 20);
- INSERT INTO user_info (name, age) VALUES ('f', 21);
- INSERT INTO user_info (name, age) VALUES ('g', 23);
- INSERT INTO user_info (name, age) VALUES ('h', 50);
- INSERT INTO user_info (name, age) VALUES ('i', 15);
- CREATE TABLE `order_info` (
- `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
- `user_id` BIGINT(20) DEFAULT NULL,
- `product_name` VARCHAR(50) NOT NULL DEFAULT '',
- `productor` VARCHAR(30) DEFAULT NULL,
- PRIMARY KEY (`id`),
- KEY `user_product_detail_index` (`user_id`, `product_name`, `productor`)
- )
- ENGINE = InnoDB
- DEFAULT CHARSET = utf8
- INSERT INTO order_info (user_id, product_name, productor) VALUES (1, 'p1', 'WHH');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (1, 'p2', 'WL');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (1, 'p1', 'DX');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (2, 'p1', 'WHH');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (2, 'p5', 'WL');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (3, 'p3', 'MA');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (4, 'p1', 'WHH');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (6, 'p1', 'WHH');
- INSERT INTO order_info (user_id, product_name, productor) VALUES (9, 'p8', 'TE');
MYSQL实例
EXPLAIN 命令的输出内容大致如下:
- mysql> explain select * from user_info where id = 2\G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
MYSQL实例
各列的含义如下:
MYSQL实例接下来我们来重点看一下比较重要的几个字段.
select_type
select_type 表示了查询的类型, 它的常用取值有:
MYSQL实例最常见的查询类别应该是 SIMPLE 了, 比如当我们的查询没有子查询, 也没有 UNION 查询时, 那么通常就是 SIMPLE 类型, 例如:
- mysql> explain select * from user_info where id = 2\G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
MYSQL实例如果我们使用了 UNION 查询, 那么 EXPLAIN 输出 的结果类似如下:
- mysql> EXPLAIN (SELECT * FROM user_info WHERE id IN (1, 2, 3))
- -> UNION
- -> (SELECT * FROM user_info WHERE id IN (3, 4, 5));
- +----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
- | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
- | 1 | PRIMARY | user_info | NULL | range | PRIMARY | PRIMARY | 8 | NULL | 3 | 100.00 | Using where |
- | 2 | UNION | user_info | 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)
MYSQL实例table
表示查询涉及的表或衍生表
type
type 字段比较重要, 它提供了判断查询是否高效的重要依据依据. 通过 type 字段, 我们判断此次查询是 全表扫描 还是 索引扫描 等.
type 常用类型
type 常用的取值有:
MYSQL实例例如下面的这个查询, 它使用了主键索引, 因此 type 就是 const 类型的.
- mysql> explain select * from user_info where id = 2\G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
MYSQL实例
- mysql> EXPLAIN SELECT * FROM user_info, order_info WHERE user_info.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: user_info
- 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)
MYSQL实例例如下面这个例子中, 就使用到了 ref 类型的查询:
- mysql> EXPLAIN SELECT * FROM user_info, order_info WHERE user_info.id = order_info.user_id AND order_info.user_id = 5\G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
MYSQL实例
MYSQL实例
例如下面的例子就是一个范围查询:
- mysql> EXPLAIN SELECT *
- -> FROM user_info
- -> WHERE id BETWEEN 2 AND 8 \G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
- mysql> EXPLAIN SELECT name FROM user_info \G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
- mysql> EXPLAIN SELECT age FROM user_info WHERE age = 20 \G
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: user_info
- 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)
上面的例子是从表 order_info 中查询指定的内容, 而我们从此表的建表语句中可以知道, 表 order_info 有一个联合索引:
- 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)
不过此查询语句 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.
- 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 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)
- 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)
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5783.html