mybatis sql 解决 in 参数过多的问题
sql in 是用内层驱动外层,一般用在数据量比较小的时候,而且条件上有索引,也会走到索引。但是如果in里的参数过多,mysql可能会放弃走索引,进而进行全表扫描,影响效率。这种情况可以把in里的参数拆分,使得sql重新走上索引,多个结果走索引后再合并,也比全表扫描快。
<if test="xxxList != null and xxxList.size() > 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>
下一篇:
mysql8.0版本重置root密码