参考文章: https://blog.csdn.net/weixin_39989941/article/details/113287244
个人学到的地方:
- 单独查询的情况都走了 ref 类型的索引,但是使用 or 关键字后sql的索引会失效。
索引失效
explain select * from test1 where code = ‘001’ or height = 8;
索引有效
explain select * from test1 where code = ‘001’;
explain select * from test1 where height = 8;
解决方式:
explain (select * from test1 where code = ‘001’) union (select * from test1 where height = 8);
以前没有注意过用 union 可解决用OR索引失效的问题
- 使用不等于号会导致索引失效
explain select * from test1 where height!=8;
5.7中这种情况sql执行结果是全表扫描,而5.8中使用了 range 类型索引