Skip to content
Advertisement

JPA and 1000 ID use in Oracle IN Operator

I use NamedNativeQuery for delete rows, and it’s somthing like this:

DELETE from FAKTOR
 where ID IN (
   select fa.ID
   from FAKTOR fa
     left join FAKTOR_REASON fars
       on fa.FARS_ID = fars.ID
   where fars.ID = 63
         and fa.USER_ID in (:userIds))

But How i can use more that 1000 userIds with IN Operator at Oracle where clues?

P.S: I’m looking for a solution to handle it in one commit;

Advertisement

Answer

Working around the IN limit is inefficient and JPA is not always the right tool for the job. Consider the following:

  1. Thousands of bound values will result in potentially megabytes of SQL. It will take a long time to send this SQL to the database. The Database might take longer to read the SQL text than execute it as per Tom’s answer to “Limit and conversion very long IN list: WHERE x IN ( ,,, …)” question.

  2. It will be inefficient due to SQL parsing. Not only does it take a long time to parse this long SQL but each invocation has a different number of bound parameters which will be parsed and planned separately (see this article which explains it).

  3. There is a hard limit of bound parameters in a SQL statement. You can repeat the OR a few times to work around the IN limit but you are going to hit the SQL statement limit at some point.

For those types of queries it’s usually better to create temporary tables. Create one before your query, insert all the identifiers into it and join it with the entity table in your query to simulate the IN condition.

Ideally, you can replace the JPA with a stored procedure, especially if you are pulling out tens of thousands of identifiers from the database just to pass them back to the database in the next query.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement