mysql范围查询索引_mysql 组合索引中对范围的查询

建立表:

20180701213836791409.png

CREATE TABLE `ygzt_test` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

`a` int(11) NOT NULL,

`b` int(11) NOT NULL,

`c` int(11) NOT NULL,

`d` int(11) NOT NULL,

PRIMARY KEY (`id`),

KEY `a` (`a`,`b`,`c`,`d`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT=‘测试‘;

一、实验一,无order by

首先加联合索引a,b,c,d

explain select * from ygzt_test where a=1 and b=2 and c=3 and d=4

20180701213836953525.png

修改sql:

explain select * from ygzt_test where a>1 and b=2 and c=3 and d=4

20180701213837178143.png

type已经由ref降为index

修改索引b,c,d,a

explain select * from ygzt_test where a>1 and b=2 and c=3 and d=4

20180701213837520929.png

done

二、实验二,order by

建立索引a,b

explain select * from ygzt_test where a>0 order by b

20180701213837790471.png

可以看到,a>0使用了索引,order by b 未使用

修改索引为b,a

explain select * from ygzt_test where a>0   order by b

823944-20180701210732590-1464360300.png

20180701213838168415.png

where 与 order by 都无索引

想起此前order by+select *的问题

这个问题单独拿出来实践下:

修改索引为a

explain select * from ygzt_test order by a

20180701213838405729.png

explain select * from ygzt_test FORCE INDEX (a)  order by a

20180701213838818831.png

结论:

1.单列索引中——,>=,between,like(右边模糊)适用索引

2.索引中有范围的,有序性失效,解决方案以实际为准

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值