postgreSQL - in vs any

PRO*_*ima 26 postgresql operation any sql-in

我试过了两个

1)smthng = any(从exmplTable中选择id)

2)smthng in(从exmplTable中选择id)

我的数据得到了相同的结果.

这两个表达有什么不同吗?

Pav*_*ule 24

不,在这些变体中是相同的:

你可以看到 - 执行计划也是一样的:

postgres=# explain select * from foo1 where id in (select id from foo2);
????????????????????????????????????????????????????????????????????
?                            QUERY PLAN                            ?
????????????????????????????????????????????????????????????????????
? Hash Semi Join  (cost=3.25..21.99 rows=100 width=4)              ?
?   Hash Cond: (foo1.id = foo2.id)                                 ?
?   ->  Seq Scan on foo1  (cost=0.00..15.00 rows=1000 width=4)     ?
?   ->  Hash  (cost=2.00..2.00 rows=100 width=4)                   ?
?         ->  Seq Scan on foo2  (cost=0.00..2.00 rows=100 width=4) ?
????????????????????????????????????????????????????????????????????
(5 rows)

postgres=# explain select * from foo1 where id = any (select id from foo2);
????????????????????????????????????????????????????????????????????
?                            QUERY PLAN                            ?
????????????????????????????????????????????????????????????????????
? Hash Semi Join  (cost=3.25..21.99 rows=100 width=4)              ?
?   Hash Cond: (foo1.id = foo2.id)                                 ?
?   ->  Seq Scan on foo1  (cost=0.00..15.00 rows=1000 width=4)     ?
?   ->  Hash  (cost=2.00..2.00 rows=100 width=4)                   ?
?         ->  Seq Scan on foo2  (cost=0.00..2.00 rows=100 width=4) ?
????????????????????????????????????????????????????????????????????
(5 rows)

  • 请注意,虽然对于采用*set*的表单来说这是正确的,但每个`IN()`和`= ANY()`都有第二种形式,这些形式并不完全等效.请考虑:http://stackoverflow.com/a/34627688/939860和http://dba.stackexchange.com/q/125413/3684 (2认同)

Wig*_*rld 11

这可能是一个极端情况,但:

select * from myTable where id IN ()
Run Code Online (Sandbox Code Playgroud)

将产生:错误:“)”或附近的语法错误

select * from myTable where id = ANY('{}');
Run Code Online (Sandbox Code Playgroud)

将返回一个空结果集