直接上干货
<if test="parentIdList !=null and parentIdList.size() > 0">
AND PARENT_ID IN
<!--第一种:每个元素union all一次-->
<foreach item="parentId" index="index" collection="parentIdList" open="(" separator=" union all " close=")">
select '${parentId}' from dual
</foreach>
<!--第二种:每999个or一次-->
<foreach collection="parentIdList" item="parentId" index="index" open="(" close=")" separator=",">
<if test="(index % 999) == 998"> NULL )
OR PARENT_ID IN (
</if>#{parentId}
</foreach>
<!--第三种:每999个union all一次-->
<foreach collection="parentIdList" item="parentId" index="index" open="(" close=")" separator=",">
<if test="(index % 999) == 998">NULL) UNION ALL SELECT <include refid="base_column_list"/>
FROM USER WHERE PARENT_ID IN(
</if>#{parentId}
</foreach>
</if>
注意: base_column_listsql块要和你主查询的字段一致
文章介绍了在SQL查询中处理大量数据时,针对IN子句的三种优化策略:一是每个元素用UNIONALL,二是每999个元素用OR,三是每999个元素用UNIONALL。这些方法旨在避免单个IN子句包含过多元素导致的性能问题。
1144

被折叠的 条评论
为什么被折叠?



