后台报了一些异常日志,查阅后发现在 oracle 数据库中使用 in 关键字条件不能超过 1000 个,当时写查询语句时没有关注这个问题

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

总结一下解决方法

1.分多次查询,对查询要求不高的话。把入参的集合按照每个最大1000个来处理,分几次查询,然后把结果进行汇总,这样就只用改动代码,不用改动SQL。

2.把参数分开但还是一次查询,使用 or 连接

select * from table where id in (1, 2, …, 1000) or id in (1001, …, 1999)

3.在 in 后面接一个查询语句

select * from A where id in (select id from B)

4.与 3 类似,使用 with  as 语法,把条件封装成一个表

with temp as (select * from A)
select * from B where id in (select id from temp)

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄