Sql – Most efficient way to deal with ORA-01795:maximum number of expressions in a list is 1000 in hibernate

hibernateoraclesql

I have to perform a select on which I have more than 1000 elements via hibernate, and then I received the error "ORA-01795:maximum number of expressions in a list is 1000" when I'm using the Oracle brand.

SELECT * FROM matable WHERE column IN (?,?,...) (>1000 items) 

I found many solutions :
Split the list with OR

where A in (a,b,c,d,e,f)

becomes

where (A in (a,b,c) OR a in (d,e,f)) ...

Create a table with UNION ALL

SELECT * FROM maintable
JOIN (
    SELECT v1 a FROM DUAL UNION ALL
    SELECT v2 a FROM DUAL UNION ALL
    SELECT v3 a FROM DUAL UNION ALL
    ...
    SELECT v2000 a FROM DUAL) tmp

    on tmp.a = maintable.id

Using tuples to get rid of the limit

where (column,0) in ((1,0),(2,0),(3,0),(4,0), ... ,(1500,0))

Using a temporary table..

where A in SELECT item FROM my_temporary_table

References here and there and also there.

My question is the following : what is the best practice to deal with this issue? By best practice I mean the most performant, but not only for Oracle; if I use hibernate, I don't want to create and manage a different code for each brand of database (I'm concerned by Oracle, MS SQL and PostGre only).

My first reaction would have been to use a temporary table, but I don't know what has the most impact.

Best Answer

Use a temporary table and make the values primary keys on the table. This should allow very efficient optimizations for comparison. The most like is simply an index lookup, although if the table is very small, Oracle might choose some other method such as a table scan.

This method should be faster than 1,000 or conditions, in almost any database. Sometimes in is optimized in a similar way (using a binary tree to store the values). In such databases, the performance would be similar.