使用过Oracle的可能都遇到过In不能超过1000的问题,SQL中超过1000会直接报错。这里分享几个方案来解决这个问题。

方法一:

在in里面使用select ‘固定值’ from dual + union all 的子查询。

这种方法的优势是对原有业务逻辑改动最小。例如:

select i.*
from table1 i 
where field1 in (
  select '1' from dual
  union all 
  select '2' from dual
  union all 
  select '3' from dual
)

结合Mybatis可以这么写。

PS:最好别用#{},经尝试druid解析占位符过多的时候会报错。我的druid版本是:1.0.11



<if test="@Ognl@isNotEmpty(codeList)">
    and i.code in 
    <foreach item="code" index="index" collection="codeList" open="(" separator=" union all " close=")">
        select '${code}' from dual
    </foreach>
</if>

方法二:

跟上面方法类似,但把子查询用 with as封装起来。这种方式如果in条件在很多地方用到,性能会提高。例如:

with t as (
    select '1' as code from dual
    union all
    select '2' as code from dual
)
select i.*
from table1 i
where i.code in (
    select t.code from t
)

方法三

新建会话级临时表,再查询之前把数据都插入到表里,然后再In查询。这种方式对业务逻辑改动不大,但需要多出了建表和插入数据的步骤,最好做下性能测试验证性能是否有问题。

方法四

使用 a IN (1,2,...,999) or a in (1000,1001,...1999) or ....这种方式性能相对不高。而且需要改动业务逻辑。不推荐。

方法五

在Java中按1000分批查询,再把结果加起来,这种方式稍复杂,而且查询次数过多,对业务逻辑改动较大,最好让测试测下超过1000的场景。不推荐。
 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐