小编zzz*_*zzz的帖子

从postgres中选择记录,其中时间戳在一定范围内

我在表保留中有时间戳类型的到达列(我正在使用postgres).例如,我如何选择今年的所有日期?

我知道我可以这样做:

select * FROM reservations WHERE extract(year from arrival) = 2012;
Run Code Online (Sandbox Code Playgroud)

但是我运行了分析,看起来它需要序列扫描.有更好的选择吗?

PS 1嗯.两种方式似乎都需要seq.扫描.但是wildplasser的那个产生的结果更快 - 为什么?

cmm=# EXPLAIN ANALYZE select * FROM reservations WHERE extract(year from arrival) = 2010;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Seq Scan on vrreservations  (cost=0.00..165.78 rows=14 width=4960) (actual time=0.213..4.509 rows=49 loops=1)
   Filter: (date_part('year'::text, arrival) = 2010::double precision)
 Total runtime: 5.615 ms
(3 rows)

cmm=# EXPLAIN ANALYZE SELECT * from reservations WHERE arrival > '2010-01-01 00:00:00' AND arrival < '2011-01-01 00:00:00';
                                                                  QUERY PLAN                                                                   
-----------------------------------------------------------------------------------------------------------------------------------------------
 Seq Scan …
Run Code Online (Sandbox Code Playgroud)

sql postgresql

48
推荐指数
3
解决办法
8万
查看次数

标签 统计

postgresql ×1

sql ×1