postgresql - Postgres : Queries with INTERVAL operation don work over constraints

标签 postgresql timestamp intervals

为了简化,我有以下查询:

SELECT * FROM table WHERE (DATE_TRUNC('day',create_time ) > now() - interval '2 days');

运行解释我得到了这个:

->  Seq Scan on table  (cost=0.00..1.62 rows=10 width=232)
               Filter: (date_trunc('day'::text, create_time) > (now() - '2 days'::**interval**))

正如我强调的那样,此操作 (now() - interval '2 days') 返回一个间隔,但我需要它是一个时间戳。

在这种情况下,我如何将时间间隔转换为时间戳或类似的东西?


Thank you all for answers, but I think it is not very well explained, here's the detailed problem:

出于性能目的,我们在这里有一个名为“transactions”的表和每一天的子表,例如“transactions_2015_05_29”。

在每个子表上我们有以下约束:

CONSTRAINT transactions_2015_05_29_create_time_check CHECK (date_trunc('day'::text, create_time) = '2015-05-29 00:00:00'::timestamp without time zone)

当我们运行以下“解释”以下查询时,我们得到:

解释:

explain SELECT * FROM pp_transactions WHERE (DATE_TRUNC('day', create_row_time) < current_date + interval '1 day');

"Result  (cost=0.00..120.52 rows=731 width=232)"
"  ->  Append  (cost=0.00..120.52 rows=731 width=232)"
"        ->  Seq Scan on transactions  (cost=0.00..1.70 rows=10 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))"
"        ->  Seq Scan on **transactions_2015_05_28** pp_transactions  (cost=0.00..14.65 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)"
"        ->  Seq Scan on **transactions_2015_05_29** transactions  (cost=0.00..16.98 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))"
"        ->  Seq Scan on **transactions_2015_05_30** transactions  (cost=0.00..16.98 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))"
"        ->  Seq Scan on **transactions_2015_05_31** transactions  (cost=0.00..16.98 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < (('now'::text)::date + '1 day'::interval))"

如您所见,有些表不应该存在。 但是,如果我们运行以下查询,我会在解释时得到正确的结果:

解释:

explain select * FROM pp_transactions WHERE (DATE_TRUNC('day', create_row_time) < '2015-05-30 00:00:00');

"Result  (cost=0.00..30.76 rows=216 width=232)"
"  ->  Append  (cost=0.00..30.76 rows=216 width=232)"
"        ->  Seq Scan on transactions  (cost=0.00..1.46 rows=10 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)"
"        ->  Seq Scan on **transactions_2015_05_28** pp_transactions  (cost=0.00..14.65 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)"
"        ->  Seq Scan on **transactions_2015_05_29** pp_transactions  (cost=0.00..14.65 rows=103 width=232)"
"              Filter: (date_trunc('day'::text, create_time) < '2015-05-30 00:00:00'::timestamp without time zone)"

因此,第一个查询的行为必须与第二个查询相同。

最佳答案

来自 postgres manual :

  1. You may add or subtract an INTERVAL to a TIMESTAMP to produce another TIMESTAMP

TIMESTAMP '1999-12-11' + INTERVAL '19 days' = TIMESTAMP '1999-12-30'

这对我来说像是一个约会(2 天前 19:08 CEST),并且根据手册(再次)now()产生一个时间戳:

 (now() - '2 days'::**interval**)

now() 是一个函数,“2 天”是一个间隔。

关于postgresql - Postgres : Queries with INTERVAL operation don work over constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30535032/

相关文章:

postgresql - 每个事务的 Postgres 递增 SERIAL 列

java - getTimeInMillis() 为相同的日期返回不同的值

具有特定键的 Django ManyToMany 反向查询

MySql Select Distinct 有两个表并按时间戳

JavaScript 写一个带有过期时间的 cookie

C:如何将 float 包装到区间 [-pi, pi)

postgresql - 从 PostgreSQL 中的时间间隔获取特定星期几(周末)的数量

r - Inner_join 有两个条件和区间内的区间条件

sql - 优化 Postgres 查询

python - Django Postgres Trigram 搜索 - 如何查看每个结果的分数?