oracle where in limitation to 1000 / hibernate
Oracle knows the limitation to 1000 elements in the where a in (..)
clause. Does thi开发者_如何学JAVAs limitation also exist when using Hibernate in conjuction with Oracle?
This database limitation still exists with hibernate. If you really need to have more than 1000 items in your in clause you will have to split the list yourself in the code and run the query for each block of 1000 keys, then append the result sets together.
Note this hack breaks down if your query needs to sort or otherwise aggregate the results of the query because the full set of results will only be known in the code. In this case you are better of finding another way to write the query that does not require an IN
clause.
We had the same issue and solved it by splitting it up into 100er packages of IN clause
select * from mytable where id in (...) or id in (...).
Remarks:
- make sure that you use bind variables
- make sure that the query always looks the same. This is done by filling up the IN clauses with -1 until you have 100 elements in it
- try to use always the same number of ORed in(...) so that the query always looks to same
Why you want the points above? It is so that the query optimizer can reuse the query plan.
Additional optimization:
- instead of using -1, you can just use the very last true value again. That is even a little bit better.
example: in(1, 2, 3, 4, 4, ..., 4)
Note also: we tested with various fixed numbers of elements in the in clause and observed decreased performances for a lot than 100 elements.
Yes as Hibernate will call Oracle at some stage so the limit is the lowest of the limits in Hibernate and Oracle
Hibernate does nothing special with data for an in - just passes it to the database
It seems to be a Bug in Hibernate:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1123
精彩评论