【发布时间】:2018-07-10 11:44:34
【问题描述】:
我需要在 postgres 中查找表历史记录中的行数,其中时钟列中的日期来自上个月(时钟存储为纪元时间戳)。 首先我做了:
select count(clock) from history where
date_trunc('month',to_timestamp(clock)) = date_trunc('month', CURRENT_DATE) - INTERVAL '1 month';
但这很慢。我在想如果我这样做会更快:
select count(clock) from history
where clock between {extracted first second of previous month} and {extracted last second of previous month};
当我手动输入值时,速度要快得多(我有时钟索引)。但我不知道如何提取上个月的第一和最后一秒。 提前感谢您的帮助:)
【问题讨论】:
标签: postgresql time epoch