1、索引失效的原理
Mysql索引失效的情况-****博客 可以参考这篇文章。
sql In 是用内层驱动外层,一般用在数据量比较小的时候,而且条件上有索引,一般也会走到索引。但是如果in里的参数过多,mysql可能会放弃走索引,进而进行全表扫描,影响效率。这种情况可以把in里的参数拆分,使得sql重新走上索引,多个结果走索引后再合并,也比全表扫描快。
2、解决办法
把参数拆分,从而把sql拆分成多个sql:
<if test="xxxList != null and () > 0">
and (表.字段 in
<foreach collection="xxxList" item="item" index="index" open="(" close=")">
<if test="index > 0">
<choose>
<!-- 每1000个拆分出一个or -->
<when test="(index % 1000) == 999">) OR 表.字段 in (</when>
<otherwise>,</otherwise>
</choose>
</if>
#{item}
</foreach>
)
</if>