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+树定位了,也可能是全表搜了)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值