在Postgres中,您可以指定In子句,如下所示:
SELECT * FROM user WHERE id IN (1000, 1001, 1002)
有人知道可以传递给IN的最大参数数是多少吗?
发布于 2009-06-18 00:19:59
根据找到的源代码,here, starting at line 850, PostgreSQL没有显式限制参数的数量。
下面是870行的代码注释:
/*
* We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
* possible if the inputs are all scalars (no RowExprs) and there is a
* suitable array type available. If not, we fall back to a boolean
* condition tree with multiple copies of the lefthand expression.
* Also, any IN-list items that contain Vars are handled as separate
* boolean conditions, because that gives the planner more scope for
* optimization on such clauses.
*
* First step: transform all the inputs, and detect whether any are
* RowExprs or contain Vars.
*/
发布于 2017-02-15 21:54:14
这并不是对当前问题的真正答案,但它也可能对其他人有所帮助。
至少我可以断定,使用Posgresql driver 9.1,可以传递给PostgreSQL后端的值(=Short.MAX_VALUE)有32767个的技术限制。
这是一个测试"delete from x where id in (... 100k values...)“使用postgresql jdbc驱动程序:
Caused by: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 100000
at org.postgresql.core.PGStream.SendInteger2(PGStream.java:201)
发布于 2015-03-12 03:21:31
explain select * from test where id in (values (1), (2));
查询计划
Seq Scan on test (cost=0.00..1.38 rows=2 width=208)
Filter: (id = ANY ('{1,2}'::bigint[]))
但如果尝试第二个查询:
explain select * from test where id = any (values (1), (2));
查询计划
Hash Semi Join (cost=0.05..1.45 rows=2 width=208)
Hash Cond: (test.id = "*VALUES*".column1)
-> Seq Scan on test (cost=0.00..1.30 rows=30 width=208)
-> Hash (cost=0.03..0.03 rows=2 width=4)
-> Values Scan on "*VALUES*" (cost=0.00..0.03 rows=2 width=4)
我们可以看到postgres构建了临时表并与其连接
https://stackoverflow.com/questions/1009706
复制相似问题