本文缘起自《一分钟了解索引技巧》的作业题。
假设订单业务表结构为:
order(oid, date, uid, status, money, time, …)
其中:
假设订单有三种状态:0已下单,1已支付,2已完成
业务需求,查询未完成的订单,哪个SQL更快呢?
结论:方案1最慢,方案2,3,4都能命中索引
但是...
一:union all 肯定是能够命中索引的
select * from order where status=0
union all
select * from order where status=1
说明:
二:简单的in能够命中索引
select * from order where status in (0,1)
说明:
三:对于or,新版的MySQL能够命中索引
select * from order where status=0 or status=1
说明:
四、对于!=,负向查询肯定不能命中索引
select * from order where status!=2
说明:
五、其他方案
select * from order where status < 2
这个具体的例子中,确实快,但是:
六、作业
这样的查询能够命中索引么?
注:此为示例,别较真SQL对应业务的合理性。