Mysql联合索引的列命中

我们给某表A,B,C三个字段新建联合索引,顺序为ABC

# 创建测试表
create table a_test
(
  a varchar(20) default '' null,
  b varchar(20) default '' null,
  c varchar(30) default '' null,
  d varchar(40) default '' null
);
# 创建联合索引
create index table_name_a_b_c_index
  on a_test (a, b, c);
Mysql的联合索引,根据最左原则, 有a,ab,abc三个最左索引前缀可供命中

即:只要查询条件中带有a字段条件,或者同时带有ab两个字段的条件,或者同时带有abc三个字段的条件,都会使用到联合索引abc

例1:
explain select * from a_test where  a='ss' and b = 'c'  and d='c' and c='c'

该sql语句的查询条件可以组成abc索引前缀,完整的使用了abc联合索引


例2:
explain select * from a_test where  a='ss' and b = 'c'  and d='c'

该sql语句的查询条件可以组成ab索引前缀,使用了abc联合索引的ab部分


例3:
explain select * from a_test where a='ss' and d='c'

该sql语句的查询条件可以组成A索引前缀,使用了abc联合索引的 a部分


例4:
explain select * from a_test where  b = 'c'  and d='c' and c='cc'

该sql语句并未使用到abc联合索引,因为 无法组成 最左索引前缀

ubuntu无法截图,demo sql 已给出,explain分析结果可以自行执行之后进行查看,同时也可以熟悉explain的key_lenref的字段含义

转载于:https://my.oschina.net/querying/blog/1930448

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值