PreparedStatement IN clause alternatives?

前端 未结 30 3999
情歌与酒
情歌与酒 2020-11-21 05:19

What are the best workarounds for using a SQL IN clause with instances of java.sql.PreparedStatement, which is not supported for multiple values du

30条回答
  •  猫巷女王i
    2020-11-21 05:47

    I came across a number of limitations related to prepared statement:

    1. The prepared statements are cached only inside the same session (Postgres), so it will really work only with connection pooling
    2. A lot of different prepared statements as proposed by @BalusC may cause the cache to overfill and previously cached statements will be dropped
    3. The query has to be optimized and use indices. Sounds obvious, however e.g. the ANY(ARRAY...) statement proposed by @Boris in one of the top answers cannot use indices and query will be slow despite caching
    4. The prepared statement caches the query plan as well and the actual values of any parameters specified in the statement are unavailable.

    Among the proposed solutions I would choose the one that doesn't decrease the query performance and makes the less number of queries. This will be the #4 (batching few queries) from the @Don link or specifying NULL values for unneeded '?' marks as proposed by @Vladimir Dyuzhev

提交回复
热议问题