该表reports
按天分区表,例如reports_20170414
,reports_20170415
约束 SQL 定义如下
CHECK (
rpt_datetime >= '2017-04-14 00:00:00+00'::timestamp with time zone
AND
rpt_datetime < '2017-04-15 00:00:00+00'::timestamp with time zone
)
Run Code Online (Sandbox Code Playgroud)
让我们考虑两种类型的查询
SELECT SUM(rpt_unique_clicks)
FROM reports WHERE rpt_datetime >= '2017-04-14 00:00:00';
Run Code Online (Sandbox Code Playgroud)
以上查询在亚秒内运行,一切正常。
SELECT SUM(rpt_unique_clicks)
FROM reports WHERE rpt_datetime >=
date_trunc('day', current_timestamp);
Run Code Online (Sandbox Code Playgroud)
相反,上面的查询运行至少 15 秒。
SELECT date_trunc('day', CURRENT_TIMESTAMP), '2017-04-14 00:00:00';
Run Code Online (Sandbox Code Playgroud)
返回
2017-04-14 00:00:00 +00:00 | 2017-04-14 00:00:00
Run Code Online (Sandbox Code Playgroud)
当我检查为什么后者运行时间长(使用解释分析)时,我完成了它访问并扫描每个表(~500)的结果,但前者仅访问,reports_20170414
因此约束检查存在问题。
我想查询今天,而不是像后一个查询那样使用准备好的语句。为什么date_trunc('day', CURRENT_TIMESTAMP)
不等于 2017-04-14 00:00:00
?
postgresql constraint partitioning timestamp check-constraints