postgresql - 加速查询日期变量
问题描述
我需要使用脚本查询昨天每天早上生成的数据。
查询就像
select xxx
from table
where create_time >= current_date - 1
and create_time < current_date
limit 10;
它的查询计划是:
QUERY PLAN
------------------------------------------------------------------------------------
Limit (cost=100.00..117.64 rows=3 width=138)
-> Foreign Scan on table (cost=100.00..117.64 rows=3 width=138)
上面的查询需要很长时间。
但是,当我使用如下所示的固定时间时,它会立即返回结果......
select xxx
from table
where create_time >= '2020-07-30 00:00:00'
and create_time < '2020-08-03 00:00:00'
limit 10;
其查询计划为:10;
QUERY PLAN
----------------------------------------------------------------------------------------
Limit (cost=100.00..131.46 rows=3 width=138)
-> Foreign Scan on table (cost=100.00..131.46 rows=3 width=138)
Filter: ((create_time < CURRENT_DATE) AND (create_time >= (CURRENT_DATE - 1)))
是什么造成了这种差异?以及如何加速第一次查询?
解决方案
显然table
是一张外国桌子。Postgres 外部数据包装器无法将表达式下推到外部服务器,因为服务器可能对运行查询的current_date - 1
服务器有不同的想法。current_date
所以它必须从远程服务器获取所有行并在运行查询的服务器上进行过滤。
可以将常量值下推到外部服务器,以便仅返回符合条件的行(并发送网络)(很可能使用索引)。
推荐阅读
- html - 根据布尔值更改表格中的单元格文本 - Html
- javascript - 如何使用 Chart.js 进行自动平移
- php - 需要一种读取 20k 值的方法,而不是将它们放在包含 20k 项的数组中
- java - 为什么我的计数器计数不正确?尽管我的 if 语句未填写,但它增加了 +1
- google-analytics - 谷歌分析数据异常
- git - git checkout HEAD~N 不会进行我期望的提交
- javascript - 如何在Javascript(NodeJS)中将对象设置为可为空
- database - Yugabyte - INSERT/UPDATE 操作是酸性的?
- node.js - 在nodejs中循环db查询承诺的结果
- c# - 有没有办法使用
在一个 - ?