在oracle中,使用in方法查询记录的时候,如果in后面的参数个数超过1000个,那么会发生错误,JDBC会抛出“java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000”这个异常。比如执行select * from table where id in (1, 2, ..., 1000, 1001, .....,1999)时。
在网上搜了一下,解决方案都是将参数分段,即select * from table where id in (1, 2, ..., 1000) or id in(1001, ....., 1999)。
但是这种方式感觉效率太低,当参数特别多的时候可能非常慢。
请教各位大神,有没有其他的解决方案可以解决这个问题?
...全文
1974813打赏收藏
Oracle中IN参数个数超过1000的解决方案,求大神指教!
在oracle中,使用in方法查询记录的时候,如果in后面的参数个数超过1000个,那么会发生错误,JDBC会抛出“java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000”这个异常。比如执行select * from table where id in (1, 2, ..., 1000, 1001, .....,1999)时。 在网上搜了一下,解决方案都是将参数分段,即select * from table where id in (1, 2, ..
改SQL,参数太多了,只有想其他办法
比如:
select *
from table
where id in (1, 2, .. ., 1000)
union all
select *
from table
where id in (1001, .... ., 1999)
[/quote]
这种方式效率会比or的方式高吗?
另外,由于这种方式相当于把整个sql重复n次,如果原来的sql就很长很复杂,这种方式得到的sql岂不是还要成倍的增加?[/quote]
看索引怎么建的了,使用or的话很容易不走索引而使用全表扫描
改SQL,参数太多了,只有想其他办法
比如:
select *
from table
where id in (1, 2, .. ., 1000)
union all
select *
from table
where id in (1001, .... ., 1999)
[/quote]
这种方式效率会比or的方式高吗?
另外,由于这种方式相当于把整个sql重复n次,如果原来的sql就很长很复杂,这种方式得到的sql岂不是还要成倍的增加?