PreparedStatement IN clause alternatives?

前端 未结 30 4481
情歌与酒
情歌与酒 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 05:42

    An unpleasant work-around, but certainly feasible is to use a nested query. Create a temporary table MYVALUES with a column in it. Insert your list of values into the MYVALUES table. Then execute

    select my_column from my_table where search_column in ( SELECT value FROM MYVALUES )
    

    Ugly, but a viable alternative if your list of values is very large.

    This technique has the added advantage of potentially better query plans from the optimizer (check a page for multiple values, tablescan only once instead once per value, etc) may save on overhead if your database doesn't cache prepared statements. Your "INSERTS" would need to be done in batch and the MYVALUES table may need to be tweaked to have minimal locking or other high-overhead protections.

提交回复
热议问题