首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PostgreSQL - " in“子句中参数的最大数目?

PostgreSQL - " in“子句中参数的最大数目?
EN

Stack Overflow用户
提问于 2009-06-17 21:48:13
回答 7查看 128.7K关注 0票数 176

在Postgres中,您可以指定In子句,如下所示:

代码语言:javascript
运行
复制
SELECT * FROM user WHERE id IN (1000, 1001, 1002)

有人知道可以传递给IN的最大参数数是多少吗?

EN

回答 7

Stack Overflow用户

发布于 2009-06-18 00:19:59

根据找到的源代码,here, starting at line 850, PostgreSQL没有显式限制参数的数量。

下面是870行的代码注释:

代码语言:javascript
运行
复制
/*
 * 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.
 */
票数 93
EN

Stack Overflow用户

发布于 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驱动程序:

代码语言:javascript
运行
复制
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)
票数 80
EN

Stack Overflow用户

发布于 2015-03-12 03:21:31

代码语言:javascript
运行
复制
explain select * from test where id in (values (1), (2));

查询计划

代码语言:javascript
运行
复制
 Seq Scan on test  (cost=0.00..1.38 rows=2 width=208)
   Filter: (id = ANY ('{1,2}'::bigint[]))

但如果尝试第二个查询:

代码语言:javascript
运行
复制
explain select * from test where id = any (values (1), (2));

查询计划

代码语言:javascript
运行
复制
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构建了临时表并与其连接

票数 41
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1009706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档