MySQL中使用IN会不会走索引
文章很短,先看下结论,在看下文。
结论:IN肯定会走索引,但是当IN的取值范围较大时会导致索引失效,走全表扫描
navicat可视化工具使用explain函数查看sql执行信息
场景1:当IN中的取值只有一个主键时
![315ce9a3b2ebb9b793673dd6a9405e78.png](https://i-blog.csdnimg.cn/blog_migrate/66a4b154e3a62c9736ecea6d89a8bf07.png)
我们只需要注意一个最重要的type 的信息很明显的提现是否用到索引:
type结果值从好到坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > in