SQL IN Clause 1000 item limit

前端 未结 4 1610
梦毁少年i
梦毁少年i 2020-11-22 11:19

It is possible to put more than 1000 items in the SQL IN clause? We have been getting issues with our Oracle database not being able to handle it.

IF yes, how do we

相关标签:
4条回答
  • 2020-11-22 12:00

    There's another workaround for this that isn't mentioned in any of the other answers (or other answered questions):

    Any in statement like x in (1,2,3) can be rewritten as (1,x) in ((1,1), (1,2), (1,3)) and the 1000 element limit will no longer apply. I've tested with an index on x and explain plan still reports that Oracle is using an access predicate and range scan.

    0 讨论(0)
  • 2020-11-22 12:00

    Another way:

    SELECT COL1, COL2, COL3 FROM YOUR_TABLE
    WHERE 1=1
    AND COL2 IN (
    SELECT VAL1 as FAKE FROM DUAL
    UNION
    SELECT VAL2 as FAKE FROM DUAL
    UNION
    SELECT VAL3 as FAKE FROM DUAL
    --...
    )
    
    0 讨论(0)
  • 2020-11-22 12:14

    You should transform the IN clauses to INNER JOIN clauses.

    You can transform a query like this one

    SELECT  foo   
    FROM    bar   
    WHERE bar.stuff IN  
           (SELECT  stuff FROM asdf)
    

    in a query like this other one.

    SELECT  b.foo 
    FROM    ( 
            SELECT  DISTINCT stuff 
            FROM    asdf ) a 
    JOIN    bar b 
    ON      b.stuff = a.stuff
    

    You will also gain a lot of performance

    0 讨论(0)
  • 2020-11-22 12:15

    We can have more than one "IN" statement for the same variable.

    For ex:

    select val
     from table
    where val in (1,2,3,...)
    or
    val in (7,8,9,....)
    
    0 讨论(0)
提交回复
热议问题