PostgreSQL - IN 与任何
PostgreSQL - IN vs ANY
我都试过了:
smthng = ANY
(select id from exmplTable)
smthng IN
(select id from exmplTable)
我的数据得到了相同的结果。
这两个表达式有什么区别吗?
不,在这些变体中是相同的:
你可以看到 - 执行计划也是一样的:
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)
这可能是一个边缘案例,但是:
select * from myTable where id IN ()
将产生:错误:“)”处或附近的语法错误
但是
select * from myTable where id = ANY('{}');
将return一个空的结果集
注意: 已验证并正常工作
创建Table:用户
CREATE TABLE user (
id serial PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
skills VARCHAR[50]
);
插入数据
insert into user (username, skills) values ('user1', '{java, python}');
insert into user (username, skills) values ('user2', '{python}');
insert into user (username) values ('user3');
在上面的table用户中,当我们在技能栏中搜索'python'时,会出现return2行。因为它匹配前 2 行中的 python。
SELECT * FROM user where 'python' = ANY (skills);
输出
1 | user1 | {java, python}
2 | user2 | {python}
我都试过了:
smthng
= ANY
(select id from exmplTable)
smthng
IN
(select id from exmplTable)
我的数据得到了相同的结果。
这两个表达式有什么区别吗?
不,在这些变体中是相同的:
你可以看到 - 执行计划也是一样的:
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)
这可能是一个边缘案例,但是:
select * from myTable where id IN ()
将产生:错误:“)”处或附近的语法错误
但是
select * from myTable where id = ANY('{}');
将return一个空的结果集
注意: 已验证并正常工作
创建Table:用户
CREATE TABLE user (
id serial PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
skills VARCHAR[50]
);
插入数据
insert into user (username, skills) values ('user1', '{java, python}');
insert into user (username, skills) values ('user2', '{python}');
insert into user (username) values ('user3');
在上面的table用户中,当我们在技能栏中搜索'python'时,会出现return2行。因为它匹配前 2 行中的 python。
SELECT * FROM user where 'python' = ANY (skills);
输出
1 | user1 | {java, python}
2 | user2 | {python}