MySQL执行计划核心内容

MySQL执行计划核心内容

大家都知道,mysql在执行查询的时候会进行查询优化。简单来讲就是执行的时候先基于成本和规则优化生成执行计划,然后再按照执行计划执行查询。本文主要介绍EXPLAIN各输出项的含义,从而帮助大家更好的进行sql性能优化

案例表设计

-- 用户表
create table t_user (
  id            int primary key,
  login_name    varchar(100),
  name          varchar(100),
  age           int,
  sex           char(1),
  department_id int,
  address       varchar(100)
);
-- 部门表
create table t_department (
  id   int primary key,
  name varchar(100)
);
-- 地址表
create table t_address (
  id   int primary key,
  addr varchar(100)
);
-- 创建普通索引
alter table t_user
  add index idx_dep(department_id);
-- 创建唯一索引
alter table t_user
  add unique index idx_login_name(login_name);
alter table t_user
  add index idx_name_age_sex(name, age, sex);  -- 创建组合索引
alter table t_address
  add fulltext ft_address(addr);-- 创建全文索引

执行计划详解

我们可以在查询语句前面加上EXPLAIN关键字来查看这个查询的执行计划。例如

mysql> EXPLAIN SELECT 1;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | No tables used |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+1 row in set, 1 warning (0.01 sec)

可以看到,执行计划包含很多输出列,我们先简单过一下各列的大致作用,后面再进行详细讲解。

列名描述
id在一个大的查询语句中每个SELECT关键字都对应一个唯一的id
select_typeSELECT关键字对应的那个查询的类型
table表名
partitions匹配的分区信息
type针对单表的访问方法
possible_keys可能用到的索引
key实际上使用的索引
key_len实际使用到的索引长度
ref当使用索引列等值查询时,与索引列进行等值匹配的对象信息
rows预估的需要读取的记录条数
filtered某个表经过搜索条件过滤后剩余记录条数的百分比
Extra一些额外的信息

列详解

id

每个 SELECT语句都会自动分配的一个唯一标识符.
表示查询中操作表的顺序,有三种情况:
id相同:执行顺序由上到下 id不同:如果是子查询,id号会自增,id越大,优先级越高。 id相同的不同的同时存在
id列为null的就表示这是一个结果集,不需要使用它来进行查询。

select_type(重要)

查询类型,主要用于区别普通查询、联合查询(union、union all)、子查询等复杂查询。

simple

表示不需要union操作或者不包含子查询的简单select查询。有连接查询时,外层的查询为simple,且只有一个

mysql> explain select * from t_user;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

primary

一个需要union操作或者含有子查询的select,位于最外层的单位查询的select_type即为primary。且只有一个

mysql> explain select (select name from t_user) from t_user ;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | PRIMARY     | t_user | index | NULL          | idx_dep | 5       | NULL |    1 | Using index |
|  2 | SUBQUERY    | t_user | ALL   | NULL          | NULL    | NULL    | NULL |    1 | NULL        |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
2 rows in set (0.00 sec)

subquery

除了from字句中包含的子查询外,其他地方出现的子查询都可能是subquery

mysql> explain select * from t_user where  id = (select max(id) from t_user);
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | PRIMARY     | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
|  2 | SUBQUERY    | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No matching min/max row                             |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
2 rows in set (0.01 sec)

dependent subquery

与dependent union类似,表示这个subquery的查询要受到外部表查询的影响

mysql> explain select id,name,(select name from t_department a where a.id=b.department_id) from t_user b;
+----+--------------------+-------+--------+---------------+---------+---------+------------------------+------+-------+
| id | select_type        | table | type   | possible_keys | key     | key_len | ref                    | rows | Extra |
+----+--------------------+-------+--------+---------------+---------+---------+------------------------+------+-------+
|  1 | PRIMARY            | b     | ALL    | NULL          | NULL    | NULL    | NULL                   |    1 | NULL  |
|  2 | DEPENDENT SUBQUERY | a     | eq_ref | PRIMARY       | PRIMARY | 4       | sample.b.department_id |    1 | NULL  |
+----+--------------------+-------+--------+---------------+---------+---------+------------------------+------+-------+

union

union连接的两个select查询,第一个查询是PRIMARY,除了第一个表外,第二个以后的表select_type 都是union

mysql> explain select * from t_user where sex='1'  union select * from t_user
    -> where sex='2';
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------+
| id | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows | Extra           |
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------+
|  1 | PRIMARY      | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where     |
|  2 | UNION        | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where     |
| NULL | UNION RESULT | <union1,2> | ALL  | NULL          | NULL | NULL    | NULL | NULL | Using temporary |
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------+

dependent union

与union一样,出现在union 或union all语句中,但是这个查询要受到外部查询的影响

mysql> explain select * from t_user where sex in (select sex from t_user where sex='1' union select sex from t_user where sex='2');
+----+--------------------+------------+------+---------------+------+---------+------+------+-----------------+
| id | select_type        | table      | type | possible_keys | key  | key_len | ref  | rows | Extra           |
+----+--------------------+------------+------+---------------+------+---------+------+------+-----------------+
|  1 | PRIMARY            | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where     |
|  2 | DEPENDENT SUBQUERY | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where     |
|  3 | DEPENDENT UNION    | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where     |
| NULL | UNION RESULT       | <union2,3> | ALL  | NULL          | NULL | NULL    | NULL | NULL | Using temporary |
+----+--------------------+------------+------+---------------+------+---------+------+------+-----------------+

union result

包含union的结果集,在union和union all语句中,因为它不需要参与查询,所以id字段为null

derived

from字句中出现的子查询,也叫做派生表,其他数据库中可能叫做内联视图或嵌套select

mysql> explain select * from (select * from t_user where sex='1') b;
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL        |
|  2 | DERIVED     | t_user     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
table
  • 显示的查询表名,如果查询使用了别名,那么这里显示的是别名
  • 如果不涉及对数据表的操作,那么这显示为null
  • 如果显示为尖括号括起来的就表示这个是临时表,后边的N就是执行计划中的id,表示结果来自于 这个查询产生。
    如果是尖括号括起来的<union M,N>,与类似,也是一个临时表,表示这个结果来自于union查 询的id为M,N的结果集。

type总结

依次从好到差:

system,const,eq_ref,ref,fulltext,ref_or_null,unique_subquery, index_subquery,range,index_merge,index,ALL

除了all之外,其他的type都可以使用到索引,除了index_merge之外,其他的type只可以用到一个索 引。
优化器会选用最优索引 一个

注意事项:

最少要索引使用到range级别。

Type列详解

system

表只有一行记录(等于系统表),这是const类型的特列,平时不大会出现,可以忽略

mysql> explain select * from (select * from t_user where id=1) a;
+----+-------------+------------+--------+---------------+------+---------+------+------+--------------------------------+
| id | select_type | table      | type   | possible_keys | key  | key_len | ref  | rows | Extra                          |
+----+-------------+------------+--------+---------------+------+---------+------+------+--------------------------------+
|  1 | PRIMARY     | <derived2> | system | NULL          | NULL | NULL    | NULL |    0 | const row not found            |
|  2 | DERIVED     | NULL       | NULL   | NULL          | NULL | NULL    | NULL | NULL | no matching row in const table |
+----+-------------+------------+--------+---------------+------+---------+------+------+--------------------------------+

const(重要)

使用唯一索引或者主键,返回记录一定是1行记录的等值where条件时,通常type是const。其他数据库
也叫做唯一索引扫描

mysql> explain select * from t_user where id=1;
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+

eq_ref(重要)

关键字:连接字段主键或者唯一性索引。
此类型通常出现在多表的 join 查询, 表示对于前表的每一个结果, 都只能匹配到后表的一行结果. 并且查 询的比较操作通常是 ‘=’, 查询效率较高.

mysql> explain select a.id from t_user a left join t_department b on a.department_id=b.id;
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref                    | rows | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+
|  1 | SIMPLE      | a     | index  | NULL          | idx_dep | 5       | NULL                   |    1 | Using index |
|  1 | SIMPLE      | b     | eq_ref | PRIMARY       | PRIMARY | 4       | sample.a.department_id |    1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+

select * from a,b where a.id=b.id (等值连接)

select * from a where name=‘zs’ (条件查询)

ref(重要)

针对非唯一性索引,使用等值(=)查询非主键。或者是使用了最左前缀规则索引的查询。

-- 非唯一索引
mysql> explain select * from t_user where department_id=1;
+----+-------------+--------+------+---------------+---------+---------+-------+------+-------+
| id | select_type | table  | type | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+--------+------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t_user | ref  | idx_dep       | idx_dep | 5       | const |    1 | NULL  |
+----+-------------+--------+------+---------------+---------+---------+-------+------+-------+

-- 等值非主键连接
mysql> explain select a.id from t_user a left join t_department b on a.name=b.name;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                              |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
|  1 | SIMPLE      | a     | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL                                               |
|  1 | SIMPLE      | b     | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+

-- 最左前缀
mysql> explain select * from t_user where name = 'kojon';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

fulltext

全文索引检索,要注意,全文索引的优先级很高,若全文索引和普通索引同时存在时,mysql不管代价,优先选择使用全文索引

# fulltext
mysql> explain select * from t_address where match(addr) against('beijing');
+----+-------------+-----------+----------+---------------+------------+---------+------+------+-------------+
| id | select_type | table     | type     | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+-----------+----------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | t_address | fulltext | ft_address    | ft_address | 0       | NULL |    1 | Using where |
+----+-------------+-----------+----------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec)

ref_or_null

与ref方法类似,只是增加了null值的比较。实际用的不多。

unique_subquery

用于where中的in形式子查询,子查询返回不重复值唯一值

index_subquery

用于in形式子查询使用到了辅助索引或者in常数列表,子查询可能返回重复值,可以使用索引将子查询 去重。

range(重要)

索引范围扫描,常见于使用>,<,is null,between ,in ,like等运算符的查询中。

mysql> # range(重要)
mysql> explain select * from t_user where id>1;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | range | PRIMARY       | PRIMARY | 4       | NULL |    1 | Using where |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.01 sec)

mysql> -- like 前缀索引
mysql> explain select * from t_user where name like '%x';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> -- like 后缀索引
mysql> explain select * from t_user where login_name like  'x%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | t_user | range | idx_login_name | idx_login_name | 403     | NULL |    1 | Using index condition |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
1 row in set (0.01 sec)

index_merge

表示查询使用了两个以上的索引,最后取交集或者并集,常见and ,or的条件使用了不同的索引,官方 排序这个在ref_or_null之后,但是实际上由于要读取所个索引,性能可能大部分时间都不如range

index(重要)

关键字:条件是出现在索引树中的节点的。可能没有完全匹配索引。
索引全表扫描,把索引从头到尾扫一遍,常见于使用索引列就可以处理不需要读取数据文件的查询、可 以使用索引排序或者分组的查询。

mysql> -- 单索引
mysql> explain select login_name from t_user;
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key            | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | index | NULL          | idx_login_name | 403     | NULL |    1 | Using index |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+

mysql> -- 组合索引
mysql> explain select age from t_user;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

mysql> #需要通过覆盖索引将ALL类型变成index
mysql> explain select login_name,age from t_user;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

all(重要)

这个就是全表扫描数据文件,然后再在server层进行过滤返回符合要求的记录。

mysql> # all(重要)
mysql> explain select * from t_user;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

回表查询
思考:如何使用索引?

possible_keys

此次查询中可能选用的索引,一个或多个

key

查询真正使用到的索引,select_type为index_merge时,这里可能出现两个以上的索引,其他的 select_type这里只会出现一个。

key_len

  • 用于处理查询的索引长度,如果是单列索引,那就整个索引长度算进去,如果是多列索引,那么查
    询不一定都能使用到所有的列,具体使用到了多少个列的索引,这里就会计算进去,没有使用到的
    列,这里不会计算进去。
  • 留意下这个列的值,算一下你的多列索引总长度就知道有没有使用到所有的列了。
  • 另外,key_len只计算where条件用到的索引长度,而排序和分组就算用到了索引,也不会计算到key_len中。

看组合索引的使用情况

ref

如果是使用的常数等值查询,这里会显示const
如果是连接查询,被驱动表的执行计划这里会显示驱动表的关联字段
如果是条件使用了表达式或者函数,或者条件列发生了内部隐式转换,这里可能显示为func

rows

这里是执行计划中估算的扫描行数,不是精确值(InnoDB不是精确的值,MyISAM是精确的值,主要原因是InnoDB里面使用了MVCC并发机制)

extra(重要)

这个列包含不适合在其他列中显示单十分重要的额外的信息,这个列可以显示的信息非常多,有几十种,常用的有

no tables used

不带from字句的查询或者From dual查询
使用not in()形式子查询或not exists运算符的连接查询,这种叫做反连接
即,一般连接查询是先查询内表,再查询外表,反连接就是先查询外表,再查询内表。

using filesort(重要)

排序时无法使用到索引时,就会出现这个。常见于order by和group by语句中说明MySQL会使用一个外部的索引排序,而不是按照索引顺序进行读取。 MySQL中无法利用索引完成的排序操作称为“文件排序”

mysql> # using filesort(重要)
mysql> explain select * from t_user order by name;
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+------+----------------+

using index(重要)

查询时不需要回表查询,直接通过索引就可以获取查询的数据。

  • 表示相应的SELECT查询中使用到了覆盖索引(Covering Index),避免访问表的数据行,效率不错!
  • 如果同时出现Using Where ,说明索引被用来执行查找索引键值
  • 如果没有同时出现Using Where ,表明索引用来读取数据而非执行查找动作。
mysql> # 全值匹配 覆盖索引
mysql> explain select name,age,sex from t_user ;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

using temporary

表示使用了临时表存储中间结果。
MySQL在对查询结果order by和group by时使用临时表
临时表可以是内存临时表和磁盘临时表,执行计划中看不出来,需要查看status变量, used_tmp_table,used_tmp_disk_table才能看出来。

distinct

在select部分使用了distinct关键字 (索引字段)

mysql> explain select distinct a.id from t_user a,t_department b where a.department_id=b.id;
+----+-------------+-------+--------+--------------------------------+---------+---------+------------------------+------+-------------------------------------------+
| id | select_type | table | type   | possible_keys                  | key     | key_len | ref                    | rows | Extra                                     |
+----+-------------+-------+--------+--------------------------------+---------+---------+------------------------+------+-------------------------------------------+
|  1 | SIMPLE      | a     | index  | PRIMARY,idx_login_name,idx_dep | idx_dep | 5       | NULL                   |    1 | Using where; Using index; Using temporary |
|  1 | SIMPLE      | b     | eq_ref | PRIMARY                        | PRIMARY | 4       | sample.a.department_id |    1 | Using index; Distinct                     |
+----+-------------+-------+--------+--------------------------------+---------+---------+------------------------+------+-------------------------------------------+

using where(重要)

表示存储引擎返回的记录并不是所有的都满足查询条件,需要在server层进行过滤。

mysql> -- 查询条件无索引
mysql> explain select * from t_user where address='beijing';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> -- 索引失效
mysql> explain select * from t_user where age=1;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> -- 索引失效
mysql> explain select * from t_user where id in(1,2);
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

查询条件中分为限制条件和检查条件,5.6之前,存储引擎只能根据限制条件扫描数据并返回,然后server层根据检查条件进行过滤再返回真正符合查询的数据。5.6.x之后支持ICP特性,可以把检查条件也下推到存储引擎层,不符合检查条件和限制条件的数据,直接不读取,这样就大大减少了存储引擎扫描的记录数量。extra列显示using index condition

img

mysql> -- 索引下推
mysql> explain select * from t_user where name='abc';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

索引失效分析

1.全值匹配我最爱

mysql> explain select * from t_user where name='kojon' and age=1 and sex='1';

2.最佳左前缀法组合索引

组合索引

带头索引不能死,中间索引不能断

如果索引了多个列,要遵守最佳左前缀法则。指的是查询从索引的最左前列开始 并且不跳过索引中的列。

错误的示例:

带头索引死:

mysql> -- 带头索引死
mysql> explain select * from t_user where age=23;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

中间索引断(带头索引生效,其他索引失效):

mysql> -- 中间索引断
mysql> explain select * from t_user where name='aa' and sex='1';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

比较

mysql> explain select * from t_user where name='aa' and sex='1' and age=23;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

mysql> explain select * from t_user where name='aa' and sex=1 and age=23;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

3.不要在索引上做计算

mysql> #不要进行这些操作:计算、函数、自动/手动类型转换,不然会导致 索引失效而转向全表扫描
mysql> explain select * from t_user where login_name='xh';
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 不要在索引上做计算
mysql> explain select * from t_user where left(login_name,1)='xh';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

4.范围条件右边的列失效

mysql> -- 范围条件右边的列失效
mysql> explain select * from t_user where name='abc' and age>20 and sex='1';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

5.尽量使用覆盖索引

mysql> -- 尽量使用覆盖索引(只查询索引的列),也就是索引列和查询列一 致,减少select *
mysql> explain select * from t_user ;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

mysql> -- 未使用覆盖索引
mysql> explain select name,login_name from t_user ;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql> -- 使用覆盖索引
mysql> explain select name,age,sex from t_user ;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql> -- 使用索引
mysql> explain select login_name  from t_user ;
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key            | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | index | NULL          | idx_login_name | 403     | NULL |    1 | Using index |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+

6.索引字段上不要使用不等

mysql> -- 索引字段上使用等于
mysql> explain select * from t_user where login_name='xh';
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 索引字段上使用不等于
mysql> explain select * from t_user where login_name!='xh';
+----+-------------+--------+------+----------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys  | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+----------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | idx_login_name | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+----------------+------+---------+------+------+-------------+

7.主键索引字段上不可以判断null

mysql> # 主键字段上不可以使用 null
mysql> explain select * from t_user where name is null;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> # 索引字段上使用 is null 判断时,可使用索引
mysql> explain select * from t_user where login_name is  null;
+----+-------------+--------+------+----------------+----------------+---------+-------+------+-----------------------+
| id | select_type | table  | type | possible_keys  | key            | key_len | ref   | rows | Extra                 |
+----+-------------+--------+------+----------------+----------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_login_name | idx_login_name | 403     | const |    1 | Using index condition |
+----+-------------+--------+------+----------------+----------------+---------+-------+------+-----------------------+

mysql> -- 主键非空 不使用索引
mysql> explain select * from t_user where id is not null;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | PRIMARY       | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

8.索引字段使用like不以通配符开头

mysql> -- 不以%开头
mysql> explain select * from t_user where login_name like 'x%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | t_user | range | idx_login_name | idx_login_name | 403     | NULL |    1 | Using index condition |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

mysql> -- 索引字段使用like以通配符开头(‘%字符串’)时,会导致索引失效而转向全表扫描
mysql> explain select * from t_user where login_name like '%x';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

由结果可知,like以通配符结束相当于范围查找,索引不会失效。与范围条件(bettween、<、>、in等)不同的是:不会导致右边的索引失效。

9.索引字段字符串要加单引号

隐式转换问题

索引字段是字符串,但查询时不加单引号,会导致索引失效而转向全表扫描

mysql> # 索引字段字符串要加单引号
mysql> explain select * from t_user where name=123;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

10.索引字段不要使用or

索引字段使用 or 时,会导致索引失效而转向全表扫描

mysql> # 索引字段不要使用or
mysql> explain select * from t_user where name='asd' or age=18;
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

总结

img

[优化总结口诀]
全值匹配我最爱,最左前綴要遵守;
带头大哥不能死,中间兄弟不能断;
索引列上少计算,范围之后全失效;
LIKE百分写最右,覆盖索引不写星;
不等空值还有or,索引失效要少用.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值