如何改进此查询以将下面显示的查询速度从30+ 秒提高到毫秒?我正在使用PostgreSQL: v 9.6.6。
我有一个时间序列表buildings.hispoint
,用于存储表中数据点的历史数据buildings.point
。
我需要Max(value) - Min(value) as aggregation_value
为大量数据点汇总不同时间范围(例如,年初至今)的数据。
事实证明,这非常缓慢,需要改进。
buildings.hispoint
(20,210,129行)CREATE TABLE buildings.hispoint (
id int,
value float,
datetime timestamp,
point_id …
CONSTRAINT foo FOREIGN KEY (point_id)
REFERENCES buildings.point (point_id),
…
);
Run Code Online (Sandbox Code Playgroud)
SELECT COUNT(datetime) AS id,
MAX(value) - MIN(value) AS aggregation_value
FROM buildings_hispoint
WHERE point_id = 44
AND buildings_hispoint.datetime BETWEEN '2018-01-01 00:00:00' AND '2018-05-02 09:18:14';
Run Code Online (Sandbox Code Playgroud)
Aggregate (cost=160967.11..160967.12 rows=1 width=16) (actual time=21713.720..21713.720 rows=1 …
Run Code Online (Sandbox Code Playgroud) postgresql performance aggregate postgresql-9.6 time-series-database query-performance