mybatis查询in的字段有索引,比如说是主键查询, 但是in的字段过多导致索引失效,

这个时候可以考虑将in的数量变少, 200以内都可以, 在数据库方面采用 foreach unionall 的方式将数据集合查询出来

1.java代码将主键的查询改为批量lis
    List<String> ids = new ArrayList();
    //将ids改为100一次查询,参考Lists.partition 用法
    List<List<String> idsList = Lists.partition(ids ,100);
   //集合查询数据,数据量比较大则采用循环union all查询
   List<xxxxEntity> all  = xxxDao.batchQueryList( List<List<String> idsList);
    
2. sql查询条件
<select id="batchQueryList" resultType="xxxx.">
    <foreach collection="idsList" item="item" index="index" separator="union all">
         select *  from table1 whdre 1 = 1
         and t1.id in
         <foreach collection="item" item="id" open="(" separator="," close=")">
                #{id}
          </foreach>
    </foreach>
 </select>


08-20 11:38