- 问题描述: 学习<<高性能MySQL>>时, p172举例
explain select * from products from actor = 'SEAN CARREY' and title like '%APOLLO%'
, 解释这里索引无法覆盖该查询的原因其中第二个是:MySQL不能在索引中执行LIKE操作
. 但是我实际发现, 即使索引执行了LIKE也是可以覆盖索引的! -
演示代码:
-- 基于MySQL版本: 8.0.16 -- 创建一个只有2个字段的表 drop table if exists products; create table products ( actor varchar(20), title varchar(20) ); -- 创建覆盖所有字段的索引, 并未指定索引长度 create index idx_actor_title on products (actor, title); -- 索引执行LIKE操作, 查看explain结果 explain select * from products where actor = 'SEAN' and title like '%APOLLO%';
- 执行结果是:
Extra是Using where; Using index
, 所以说即使执行了LIKE也还是覆盖索引 - 请问是MySQL版本的问题吗, 还是我的理解有问题? 感谢!
覆盖索引是因为表字段就2个,都被索引包含,但只用到了索引第一列
actor
,可以查看EXPLAIN
的ref
字段,只有一个const