MySQL索引失效

2020-11-28  本文已影响0人  Doooook

哪些情况需要创建索引

  1. 主键自动建立唯一索引
  2. 频繁作为查询条件的字段应该创建索引
  3. 多表关联查询中,关联字段应该创建索引 on 两边都要创建索引
  4. 查询中排序的字段,应该创建索引
  5. 频繁查找字段 覆盖索引
  6. 查询中统计或者分组字段,应该创建索引 group by

哪些情况不需要创建索引

  1. 表记录太少
  2. 经常进行增删改操作的表
  3. 频繁更新的字段
  4. where条件里使用频率不高的字段

为什么使用组合索引

MySQL 创建组合索引的规则是首先会对组合索引的最左边的,也就是第一个name字段的数据进行排序,在第一个字段的排序基础上,然后再对后面第二个的cid字段进行排序。其实就相当于实现了类似 order by name cid 这样一种排序规则。
为了节省mysql索引存储空间以及提升搜索性能,可建立组合索引(能使用组合索引就不使用单列索引

查看执行计划

介绍

MySQL 提供了一个 EXPLAIN 命令, 它可以对SELECT 语句的执行计划进行分析,并输出 SELECT 执行的详细信息,以供开发人员针对性优化。
使用explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有没有使用上了索引,有没有做全表扫描,这都可以通过explain命令来查看。
可以通过explain命令深入了解MySQL的基于开销的优化器,还可以获得很多可能被优化器考虑到的访问策略的细节,以及当运行SQL语句时哪种策略预计会被优化器采用。
EXPLAIN 命令用法十分简单,在 SELECT 语句前加上explain就可以了,例如:

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)

参数说明

expain出来的信息有10列,分别是:

id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extras

案例表

-- 用户表
create table t_user(
    id int primary key,
    login_name varchar(100),
    name varchar(100),
    age int,
    sex char(1),
    dep int,
    address varchar(100)
);
-- 部门表
create table t_dep(
    id int primary key,
    name varchar(100)
);
-- 地址表
create table t_addr(
    id int primary key,
    addr varchar(100)
);
-- 创建普通索引
alter table t_user add index idx_dep(dep);
alter table t_dep add index idx_name(name);
-- 创建唯一索引
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_addr add fulltext ft_addr(addr);

id

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 | index | NULL          | idx_name_age_sex | 312     | NULL |    1 | Using index |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+
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_dep a where a.id = b.dep) 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       | review_test.b.dep |    1 | NULL  |
+----+--------------------+-------+--------+---------------+---------+---------+-------------------+------+-------+
2 rows in set (0.00 sec)
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 |
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------+
3 rows in set (0.00 sec)
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     | index | NULL          | idx_name_age_sex | 312     | NULL |    1 | Using where; Using index |
|  3 | DEPENDENT UNION    | t_user     | index | NULL          | idx_name_age_sex | 312     | NULL |    1 | Using where; Using index |
| NULL | UNION RESULT       | <union2,3> | ALL   | NULL          | NULL             | NULL    | NULL | NULL | Using temporary          |
+----+--------------------+------------+-------+---------------+------------------+---------+------+------+--------------------------+
4 rows in set (0.00 sec)
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 |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
2 rows in set (0.00 sec)

table

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级别。

system

表中只有一行数据或者是空表(这里的“表”指的是子查询的“表”,而不是t_user表

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  |    1 | NULL  |
|  2 | DERIVED     | t_user     | const  | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
2 rows in set (0.00 sec)
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      | t_user | const | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  |
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
-- 唯一索引
mysql> explain select * from t_user where login_name = 'Rose';
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref   | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+
|  1 | SIMPLE      | t_user | const | idx_login_name | idx_login_name | 303     | const |    1 | NULL  |
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+
1 row in set (0.00 sec)
eq_ref(重要)

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

mysql> explain select a.id from t_user a left join t_dep b on a.dep=b.id;
+----+-------------+-------+--------+---------------+---------+---------+-------------------+------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref               | rows | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+------+-------------+
|  1 | SIMPLE      | a     | index  | NULL          | idx_dep | 5       | NULL              |    5 | Using index |
|  1 | SIMPLE      | b     | eq_ref | PRIMARY       | PRIMARY | 4       | review_test.a.dep |    1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+-------------------+------+-------------+
2 rows in set (0.00 sec)
ref(重要)

针对非唯一索引,使用等值(=)查询非主键。或者是使用了最左前缀规则索引 [参看:MySQL索引最左前缀原则)]的查询。

-- 非唯一索引
mysql> explain select * from t_user where dep=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  |
+----+-------------+--------+------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
-- 等值非主键连接 
mysql> explain select a.id from t_user a left join t_dep b on a.name=b.name;
+----+-------------+-------+-------+---------------+------------------+---------+--------------------+------+-------------+
| id | select_type | table | type  | possible_keys | key              | key_len | ref                | rows | Extra       |
+----+-------------+-------+-------+---------------+------------------+---------+--------------------+------+-------------+
|  1 | SIMPLE      | a     | index | NULL          | idx_name_age_sex | 312     | NULL               |    5 | Using index |
|  1 | SIMPLE      | b     | ref   | idx_name      | idx_name         | 303     | review_test.a.name |    1 | Using index |
+----+-------------+-------+-------+---------------+------------------+---------+--------------------+------+-------------+
2 rows in set (0.00 sec)
-- 最左前缀
mysql> explain select * from t_user where name = 'zhaoyun';
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref   | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
fulltext

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

 mysql> explain select * from t_addr where match(addr) against('bei');
+----+-------------+--------+----------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type     | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+----------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | t_addr | fulltext | ft_addr       | ft_addr | 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> explain select id 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 |    4 | Using where; Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+------+--------------------------+
1 row in set (0.01 sec)
-- like 前缀索引
mysql> explain select * from t_user where name like 'z%';
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys    | key              | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | t_user | range | idx_name_age_sex | idx_name_age_sex | 303     | NULL |    1 | Using index condition |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

-- 注:like '%z' 不使用索引
index_merge

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

index(重要)

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

-- 单索引
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 | 303     | NULL |    5 | Using index |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+
-- 组合索引
mysql> explain select age from t_user;
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key              | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | index | NULL          | idx_name_age_sex | 312     | NULL |    5 | Using index |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+

-- 思考:explain select login_name,age from t_user;
-- 覆盖索引
ALL(重要)

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

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 |    5 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
-- 回表查询

possible_keys

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

key

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

key_len

ref

extra(重要)

这个列包含不适合在其他列中显示但十分重要的额外的信息,这个列可以显示的信息非常多,有几十种,常用的有:distinct、no tables used、using filesort(重要)、using index(重要)、using join buffer(block nested loop)、using join buffer(batched key accss)、using sort_union、using_union、using intersect、using sort_intersection、using temporary、using where(重要)、firstmatch(tb_name)、loosescan(m..n)、filtered

distinct

在select部分使用了distinct关键字

no tables used

不带from字句的查询或者From dual查询

mysql> explain select 1 from dual;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+

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

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

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

-- 全值匹配,覆盖索引
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 | index | NULL          | idx_name_age_sex | 312     | NULL |    5 | Using index |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+
-- 对比select * from ...
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 |    5 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
-- 索引用来读取数据而非执行查找动作
mysql> explain select id 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 |    4 | Using where; Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+------+--------------------------+
using join buffer(block nested loop),using join buffer(batched key accss)

5.6.x之后的版本优化关联查询的BNL、BKA特性。主要是减少内表的循环数量以及比较顺序的扫描查询。

mysql> explain select a.id, a.name from t_user a, t_dep b where a.dep=b.id;
+----+-------------+-------+-------+---------------+----------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type  | possible_keys | key      | key_len | ref  | rows | Extra                                              |
+----+-------------+-------+-------+---------------+----------+---------+------+------+----------------------------------------------------+
|  1 | SIMPLE      | b     | index | PRIMARY       | idx_name | 303     | NULL |    2 | Using index                                        |
|  1 | SIMPLE      | a     | ALL   | NULL          | NULL     | NULL    | NULL |    5 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+-------+---------------+----------+---------+------+------+----------------------------------------------------+
-- 对应的表结构
-- t_user
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL,
  `login_name` varchar(100) DEFAULT NULL,
  `name` varchar(100) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` char(1) DEFAULT NULL,
  `dep` int(11) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_login_name` (`login_name`),
  KEY `idx_name_age_sex` (`name`,`age`,`sex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- t_dep
CREATE TABLE `t_dep` (
  `id` int(11) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
using sort_union,using_union,using intersect,using sort_intersection
using temporary
mysql> explain select distinct a.id from t_user a, t_dep b where a.dep=b.id;
+----+-------------+-------+-------+-----------------------------------------+----------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type  | possible_keys                           | key      | key_len | ref  | rows | Extra                                              |
+----+-------------+-------+-------+-----------------------------------------+----------+---------+------+------+----------------------------------------------------+
|  1 | SIMPLE      | b     | index | PRIMARY                                 | idx_name | 303     | NULL |    2 | Using index; Using temporary                       |
|  1 | SIMPLE      | a     | ALL   | PRIMARY,idx_login_name,idx_name_age_sex | NULL     | NULL    | NULL |    5 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+-------+-----------------------------------------+----------+---------+------+------+----------------------------------------------------+
using where(重要)
  1. 表示存储引擎返回的记录并不是所有的都满足查询条件,需要在server层进行过滤。
-- 查询条件无索引
mysql> explain select * from t_user where address='USA';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
-- 索引失效
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 |    5 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
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. 查询条件中分为限制条件和检查条件,5.6之前,存储引擎只能根据限制条件扫描数据并返回,然后server层根据检查条件进行过滤再返回真正符合查询的数据。5.6.x之后支持ICP特性,可以把检查条件也下推到存储引擎层,不符合检查条件和限制条件的数据,直接不读取,这样就大大减少了存储引擎扫描的记录数量。extra列显示using index condition
mysql> explain select * from t_user where name='Rose';
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref   | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
firstmatch(tb_name)

5.6.x开始引入的优化子查询的新特性之一,常见于where字句含有in()类型的子查询。如果内表的数据量比较大,就可能出现这个。

loosescan(m..n)

5.6.x之后引入的优化子查询的新特性之一,在in()类型的子查询中,子查询返回的可能有重复记录时,就可能出现这个。
除了这些之外,还有很多查询数据字典库,执行计划过程中就发现不可能存在结果的一些提示信息。

filtered

使用explain extended时会出现这个列,5.7之后的版本默认就有这个字段,不需要使用explain extended了。这个字段表示存储引擎返回的数据在server层过滤后,剩下多少满足查询的记录数量的比例,注意是百分比,不是具体记录数。

索引失效分析

1、全值匹配我最爱
2、最佳左前缀法则
3、不在索引列上做任何操作(计算、函数、自动or手动类型转换),会导致索引失效而转向全表扫描
4、存储引擎不能使用索引中范围条件右边的列
5、尽量使用覆盖索引(只访问索引的查询(索引列和查询列一致)),减少select *
6、MySQL在使用不等于(!=或者<>)的时候无法使用索引会导致全表扫描
7、is null, is not null也无法使用索引
8、like以通配符开头('%abc')mysql索引失效,变成全表扫描的操作(注:以通配符结尾('abc%')不会失效)
9、字符串不加引号索引失效
10、少用or,用它来连接时会索引失效

1. 全值匹配我最爱

-- 条件与索引一一对应
mysql> explain select * from t_user where name='Rose' and age=20 and sex='f';
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref               | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 312     | const,const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+

2. 最佳左前缀法则

组要针对的是组合索引而言,如果索引了多个列,要遵守最佳左前缀法则,指的是查询从索引的最左前列开始 并且不跳过索引中的列。
1、where条件列的,从索引的最左前列开始,且不跳过索引中的列
2、违背原则:未以最左前列开始,索引失效
3、违背原则:跳过索引中间列(age),只引用了部分索引
结论: where条件要满足最佳左前缀法则。 口诀:带头大哥不能死,中间兄弟不能断

-- 反例:带头大哥不死(where条件中没有组合索引的带头大哥name)
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 |    5 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
-- 中间索引断(where条件中没有age,带头索引生效,其他索引失效)
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 | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
-- 比较
mysql> explain select * from t_user where name='aa' and sex='f' and age=23;
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref               | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 312     | const,const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
-- 对比
mysql> explain select * from t_user where name='aa' and age=23 and sex='1';
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref               | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 312     | const,const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+

3. 不要在索引上做计算

不要进行这些操作:计算、函数、自动/手动类型转换,不然会导致索引失效而转向全表扫描,即使满足最左前缀原则,但where条件中使用了函数后,索引失效。

mysql> explain select * from t_user where login_name='Bob';
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+
| id | select_type | table  | type  | possible_keys  | key            | key_len | ref   | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+
|  1 | SIMPLE      | t_user | const | idx_login_name | idx_login_name | 303     | const |    1 | NULL  |
+----+-------------+--------+-------+----------------+----------------+---------+-------+------+-------+

mysql> explain select * from t_user where left(login_name, 1)='B';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

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

不能继续使用索引中范围条件(bettween、<、>、in等)右边的列

mysql> explain select * from t_user where name='Jack' and age>20 and sex='f';
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys    | key              | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | t_user | range | idx_name_age_sex | idx_name_age_sex | 308     | NULL |    1 | Using index condition |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
-- 对比
mysql> explain select name from t_user where name='Jack' and age>20 and sex='f';
+----+-------------+--------+------+------------------+------------------+---------+-------+------+--------------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref   | rows | Extra                    |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using where; Using index |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+--------------------------+

5. 尽量使用覆盖索引

尽量使用覆盖索引(只查询索引的列),也就是索引列和查询列一致,减少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 |    5 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

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 |    5 | NULL  |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+

-- 覆盖索引
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 | index | NULL          | idx_name_age_sex | 312     | NULL |    5 | Using index |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+-------------+
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 | 303     | NULL |    5 | Using index |
+----+-------------+--------+-------+---------------+----------------+---------+------+------+-------------+

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

索引字段上使用(!= 或者 < >)判断时,会导致索引失效而转向全表扫描

7. 主键索引字段上不可以判断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 | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+

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

索引字段使用like以通配符开头(‘%字符串’)时,会导致索引失效而转向全表扫描

mysql> explain select * from t_user where name like 'a%';
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys    | key              | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | t_user | range | idx_name_age_sex | idx_name_age_sex | 303     | NULL |    1 | Using index condition |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+

mysql> explain select * from t_user where name like '%a';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_user | ALL  | NULL          | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+

由结果可知,like以通配符结束相当于范围查找,索引不会失效。与范围条件(bettween、<、>、in等)不同的是:不会导致右边的索引失效。
问题:解决like ‘%字符串%’时,索引失效问题的方法? 使用覆盖索引可以解决。

mysql> explain select name, age, sex from t_user where name like '%a%';
+----+-------------+--------+-------+---------------+------------------+---------+------+------+--------------------------+
| id | select_type | table  | type  | possible_keys | key              | key_len | ref  | rows | Extra                    |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+--------------------------+
|  1 | SIMPLE      | t_user | index | NULL          | idx_name_age_sex | 312     | NULL |    6 | Using where; Using index |
+----+-------------+--------+-------+---------------+------------------+---------+------+------+--------------------------+

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

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

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  | idx_name_age_sex | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+

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 | ref  | idx_name_age_sex | idx_name_age_sex | 303     | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+

10. 索引字段不要使用or

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

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

mysql> explain select * from t_user where name='asd' and age=23;
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref         | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
|  1 | SIMPLE      | t_user | ref  | idx_name_age_sex | idx_name_age_sex | 308     | const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+

总结

组合索引索引失效总结 索引失效口诀
上一篇下一篇

猜你喜欢

热点阅读