Home > database >  Oracle error code ORA-00913 - IN CLAUSE limitation with more than 65000 values (Used OR condition fo
Oracle error code ORA-00913 - IN CLAUSE limitation with more than 65000 values (Used OR condition fo

Time:10-14

My application team is trying to fetch 85,000 values from a table using a SELECT query that is being built on the fly by their program.

SELECT * FROM TEST_TABLE
   WHERE (
        ID IN (00001,00002, ..., 01000)
   OR   ID IN (01001,01002, ..., 02000)
   ...
   OR   ID IN (84001,84002, ..., 85000)
   ));

But i am getting an error "ORA-00913 too many values".

If I reduce the in clause to only 65,000 values, I am not getting this error. Is there any limitation of values for the IN CLAUSE (accompanied by OR clause)

CodePudding user response:

The issue isn't about in lists; it is about a limit on the number of or-delimited compound conditions. I believe the limit applies not to or specifically, but to any compound conditions using any combination of or, and and not, with or without parentheses. And, importantly, this doesn't seem to be documented anywhere, nor acknowledged by anyone at Oracle.

As you clearly know already, there is a limit of 1000 items in an in list - and you have worked around that.

The parser expands an in condition as a compound, or-delimited condition. The limit that applies to you is the one I mentioned already.

The limit is 65,535 "atomic" conditions (put together with or, and, not). It is not difficult to write examples that confirm this.

The better question is why (and, of course, how to work around it).

My suspicion: To evaluate such compound conditions, the compiled code must use a stack, which is very likely implemented as an array. The array is indexed by unsigned 16-bit integers (why so small, only Oracle can tell). So the stack size can be no more than 2^16 = 65,536; and actually only one less, because Oracle thinks that array indexes start at 1, not at 0 - so they lose one index value (0).

Workaround: create a temporary table to store your 85,000 values. Note that the idea of using tuples (artificial as it is) allows you to overcome the 1000 values limit for a single in list, but it does not work around the limit of 65,535 "atomic" conditions in an or-delimited compound condition; this limit applies in the most general case, regardless of where the conditions come from originally (in lists or anything else).

More information on AskTom - you may want to start at the bottom (my comments, which are the last ones in the threads):

https://asktom.oracle.com/pls/apex/f?p=100:11:10737011707014::::P11_QUESTION_ID:9530196000346534356#9545388800346146842

https://asktom.oracle.com/pls/apex/f?p=100:11:10737011707014::::P11_QUESTION_ID:778625947169#9545394700346458835

  • Related