(1)负向条件查询不能使用索引
- select x1, x2 from order where status!=0 and stauts!=1
not in/not exists都不是好习惯
可以优化为in查询:
- select x1, x2 from order where status in(2,3)
(2)前导模糊查询不能使用索引
- select x1, x2 from order where desc like '%XX'
而非前导模糊查询则可以:
- select x1, x2 from order where desc like 'XX%'
(3)数据区分度不大的字段不宜使用索引
- select x1, x2 from user where sex=1
原因:性别只有男,女,每次过滤掉的数据很少,不宜使用索引。经验上,能过滤80%数据时就可以使用索引。对于订单状态,如果状态值很少,不宜使用索引,如果状态值很多,能够过滤大量数据,则应该建立索引。
(4)在属性上进行计算不能命中索引
- select x1, x2 from order where YEAR(date) < = '2017'
即使date上建立了索引,也会全表扫描,可优化为值计算:
- select x1,