Mysql explain的extra信息中的Using index、Using index condition和Using where

参考mysql5.7手册
https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-extra-information
https://dev.mysql.com/doc/refman/5.7/en/index-condition-pushdown-optimization.html

首先明确,覆盖索引有两种用途:
1)提供要查询的字段值,这样只搜索引就可以获取想要的字段,无需回表
2)采用B+树加速定位,提高查询速度

1.Using index
The column information is retrieved from the table using only information in the index tree
without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.

意思就是没有回表,仅用索引就获取了所有字段。标识了索引用途1)。但用没用索引进行查询加速,也就是用途2),不在Using index的考虑范围。

2.Using index condition
Tables are read by accessing index tuples and testing them first to determine whether to read full table rows. In this way, index information is used to defer (“push down”) reading full table rows unless it is necessary.

有关Index Condition Pushdown Optimization说到:
Index Condition Pushdown (ICP) is an optimization for the case where MySQL retrieves rows from a table using an index. Without ICP, the storage engine traverses the index to locate rows in the base table and returns them to the MySQL server which evaluates the WHERE condition for the rows. With ICP enabled, and if parts of the WHERE condition can be evaluated by using only columns from the index, the MySQL server pushes this part of the WHERE condition down to the storage engine. The storage engine then evaluates the pushed index condition by using the index entry and only if this is satisfied is the row read from the table. ICP can reduce the number of times the storage engine must access the base table and the number of times the MySQL server must access the storage engine.

当两个where条件,其中一个在索引一个不在时,若没有ICP优化则不采用索引,直接回表根据where条件查找;若采用ICP,则先用那个索引包括的where条件在存储引擎中进行索引过滤,然后拿着过滤后的结果回表根据另一个where条件过滤。相当于部分过滤采用了索引,所以叫Using index condition,其中一个where条件满足用途2)。

3.Using where
A WHERE clause is used to restrict which rows to match against the next table or send to the client.

进行了条件过滤,但是过滤没有用到索引直接定位。索引的用途2)是肯定没用上的。

当Using index和Using where同时存在的意思是:进行了条件过滤,但是过滤没有用到索引直接定位,但是索引提供了要查询的所有字段,所以没有回表。满足用途1),不满足用途2)。

所以Using where跟回没回表是没有关系的。

用途2)是否满足应关注explain结果的type字段,如果是const的话就肯定满足用途2)了。

https://blog.csdn.net/jeffrey11223/article/details/79100761
这篇文章说的也蛮清楚的,可以结合其例子来验证下上述说法。

总结下来就是:
Using index 没有回表
Using where 没有用B+树直接定位结果
Using index&Using where 没有回表且没有用B+树直接定位结果
Using index condition 部分where条件用B+树定位并过滤
Null 回表了且没用where条件过滤(可能是用B+树定位了,也可能是全表搜了)

你可能感兴趣的:(MySQL)