转载自: https://www.cnblogs.com/phpdragon/p/8231533.html,实践如下
- MySQL explain
https://www.cnblogs.com/xuanzhi201111/p/4175635.html
- type表示MySQL在表中找到所需行的方式,又称“访问类型”。
常用的类型有: ALL, index, range, ref, eq_ref, const, system, NULL(从左到右,性能从差到好)
-
ALL:Full Table Scan, MySQL将遍历全表以找到匹配的行
-
index: Full Index Scan,index与ALL区别为index类型只遍历索引树
-
range:只检索给定范围的行,使用一个索引来选择行
-
ref: 表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值
-
eq_ref: 类似ref,区别就在使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配,简单来说,就是多表连接中使用primary key或者 unique key作为关联条件
-
const、system: 当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将该查询转换为一个常量,system是const类型的特例,当查询的表只有一行的情况下,使用system
-
NULL: MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。
主键区间查询
mysql> EXPLAIN SELECT id, username, email, PASSWORD FROM test_user WHERE id > 8999990 AND id < 8999999;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | test_user | range | PRIMARY | PRIMARY | 8 | NULL | 7 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT count(*) FROM test_user WHERE id > 8900000 AND id < 8999999;
+----------+
| count(*) |
+----------+
| 99998 |
+----------+
1 row in set (0.03 sec)
mysql> SELECT count(*) FROM test_user WHERE id > 8999900 AND id < 8999999;
+----------+
| count(*) |
+----------+
| 98 |
+----------+
1 row in set (0.00 sec)
mysql>
查询时间跟查询的区间数据量成相对的正比增长,同时使用到了主键索引。
字符串区间查找
SELECT id, username, email, PASSWORD FROM test_user WHERE username > 'username_800000' AND `password` > '7ece221bf3f5dbddbe3c2770ac19b419';
1121471 rows in set (3.58 sec)
查询未使用索引和聚合索引,进行了全表扫描,所以查询比较的慢。
mysql> explain SELECT id, username, email, PASSWORD FROM test_user WHERE username > 'username_800000' AND `password` > '7ece221bf3f5dbddbe3c2770ac19b419';
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | test_user | ALL | index_name,index_union_name_password,index_union_name_password_status | NULL | NULL | NULL | 9702644 | Using where |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
mysql>
- username 虽然有索引,但是区间查询仍然是遍历全表
mysql> show index in test_user;
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test_user | 0 | PRIMARY | 1 | id | A | 9702644 | NULL | NULL | | BTREE | | |
| test_user | 1 | index_name | 1 | username | A | 9702644 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password | 1 | username | A | 9702644 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password | 2 | password | A | 9702644 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 1 | username | A | 9702644 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 2 | password | A | 9702644 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 3 | status | A | 9702644 | NULL | NULL | YES | BTREE | | |
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
7 rows in set (0.00 sec)
mysql> explain SELECT id, username, email, PASSWORD FROM test_user WHERE username > 'username_900000';
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | test_user | ALL | index_name,index_union_name_password,index_union_name_password_status | NULL | NULL | NULL | 9702644 | Using where |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT id, username, email, PASSWORD FROM test_user WHERE username > 'username_000000' and username < 'username_900000';
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | test_user | ALL | index_name,index_union_name_password,index_union_name_password_status | NULL | NULL | NULL | 9702644 | Using where |
+----+-------------+-----------+------+-----------------------------------------------------------------------+------+---------+------+---------+-------------+
1 row in set (0.05 sec)
mysql>
非字符串,有/无索引的区间查询对比
使用了index 遍历索引树的方式
mysql> desc test_user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| username | varchar(50) | YES | MUL | NULL | |
| email | varchar(30) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| status | tinyint(1) | YES | | 0 | |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
mysql> explain SELECT count(*) FROM test_user WHERE status > 0;
+----+-------------+-----------+-------+---------------+----------------------------------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+----------------------------------+---------+------+---------+--------------------------+
| 1 | SIMPLE | test_user | index | NULL | index_union_name_password_status | 254 | NULL | 9702644 | Using where; Using index |
+----+-------------+-----------+-------+---------------+----------------------------------+---------+------+---------+--------------------------+
1 row in set (0.00 sec)
- 新增普通的Integer字段
同样的查询,主键是range(查询需要 0.09 sec), 非主键是扫描全表的方式((需要 3.45 sec)
mysql> EXPLAIN select * from test_user where id > 900000 and id < 1000000;
+----+-------------+-----------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | test_user | range | PRIMARY | PRIMARY | 8 | NULL | 197944 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN select * from test_user where no > 900000 and no < 1000000;
+----+-------------+-----------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | test_user | ALL | NULL | NULL | NULL | NULL | 9710494 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
mysql>
- 普通字段添加索引
mysql> ALTER TABLE `test_user` ADD INDEX no_index(`no`);
Query OK, 0 rows affected (15.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in test_user;
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test_user | 0 | PRIMARY | 1 | id | A | 9710494 | NULL | NULL | | BTREE | | |
| test_user | 1 | index_name | 1 | username | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password | 1 | username | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password | 2 | password | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 1 | username | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 2 | password | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | index_union_name_password_status | 3 | status | A | 9710494 | NULL | NULL | YES | BTREE | | |
| test_user | 1 | no_index | 1 | no | A | 9710494 | NULL | NULL | YES | BTREE | | |
+-----------+------------+----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
8 rows in set (0.00 sec)
mysql>
- 再次查询,会采用range方式查询
mysql> EXPLAIN select * from test_user where no > 900000 and no < 1000000;
+----+-------------+-----------+-------+---------------+----------+---------+------+--------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+----------+---------+------+--------+-----------------------+
| 1 | SIMPLE | test_user | range | no_index | no_index | 5 | NULL | 196280 | Using index condition |
+----+-------------+-----------+-------+---------------+----------+---------+------+--------+-----------------------+
1 row in set (0.00 sec)
补充代码
ALTER TABLE test_user add COLUMN `no` int(11) DEFAULT 0;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sample_data`.`update_no`()
BEGIN
DECLARE i int;
set i = 1;
while i <= 10000000 do
UPDATE test_user set no = i where id = i;
set i = i + 1;
end WHILE;
END
call update_no();