PostgreSQL OR IN ANY

1. create table

postgres=# create table tb5 (id integer,age integer);
CREATE TABLE
postgres=# \d tb5
      Table "public.tb5"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 
 age    | integer | 
2.  insert example data

postgres=# insert into tb5 select generate_series(1,10),random()*10::integer;
INSERT 0 10
postgres=# select * from tb5;
 id | age 
----+-----
  1 |   6
  2 |   2
  3 |   1
  4 |   7
  5 |   8
  6 |   9
  7 |   0
  8 |   6
  9 |  10
 10 |   0
(10 rows)
3. test 'OR'

postgres=# select * from tb5 where age=1 or age=2 or age=7;
 id | age 
----+-----
  2 |   2
  3 |   1
  4 |   7
(3 rows)
postgres=# explain analyze select * from tb5 where age=1 or age=2 or age=7;
                                          QUERY PLAN                                           
-----------------------------------------------------------------------------------------------
 Seq Scan on tb5  (cost=0.00..85.25 rows=64 width=8) (actual time=0.008..0.010 rows=3 loops=1)
   Filter: ((age = 1) OR (age = 2) OR (age = 7))
   Rows Removed by Filter: 7
 Total runtime: 0.024 ms
(4 rows)
4. test 'IN'
postgres=# select * from tb5 where age in(1,2,7);
 id | age 
----+-----
  2 |   2
  3 |   1
  4 |   7
(3 rows)
postgres=# explain analyze select * from tb5 where age in(1,2,7);
                                          QUERY PLAN                                           
-----------------------------------------------------------------------------------------------
 Seq Scan on tb5  (cost=0.00..69.12 rows=64 width=8) (actual time=0.007..0.008 rows=3 loops=1)
   Filter: (age = ANY ('{1,2,7}'::integer[]))
   Rows Removed by Filter: 7
 Total runtime: 0.020 ms
(4 rows)

note: the 'OR' operation in explain be changed the 'ANY' operation, It can be seen that postgreSQL handle the 'OR' operation by 'ANY' operation。 

5. test 'ANY'

postgres=# select * from tb5 where age=ANY('{1,2,7}');
 id | age 
----+-----
  2 |   2
  3 |   1
  4 |   7
(3 rows)
postgres=# explain analyze select * from tb5 where age=ANY('{1,2,7}');
                                          QUERY PLAN                                           
-----------------------------------------------------------------------------------------------
 Seq Scan on tb5  (cost=0.00..69.12 rows=64 width=8) (actual time=0.008..0.009 rows=3 loops=1)
   Filter: (age = ANY ('{1,2,7}'::integer[]))
   Rows Removed by Filter: 7
 Total runtime: 0.020 ms
(4 rows)


ConClusion: The IN operation can be change the OR operation, and the OR operation can be changed the ANY operation. So, when we use the IN or OR operaion,

we can use ANY instead of them.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值