我station_logs
在PostgreSQL 9.6数据库中有一个表:
Column | Type |
---------------+-----------------------------+
id | bigint | bigserial
station_id | integer | not null
submitted_at | timestamp without time zone |
level_sensor | double precision |
Indexes:
"station_logs_pkey" PRIMARY KEY, btree (id)
"uniq_sid_sat" UNIQUE CONSTRAINT, btree (station_id, submitted_at)
我试图为每个获取level_sensor
基于的最后一个值。大约有400个唯一值,每天每个大约2万行。submitted_at
station_id
station_id
station_id
创建索引之前:
EXPLAIN ANALYZE
SELECT DISTINCT ON(station_id) station_id, submitted_at, level_sensor
FROM station_logs ORDER BY station_id, submitted_at DESC;
唯一(费用= 4347852.14..4450301.72行= 89宽度= 20)(实际时间= 22202.080..27619.167行= 98循环= 1) ->排序(cost = 4347852.14..4399076.93行= 20489916宽度= 20)(实际时间= 22202.077..26540.827行= 20489812循环= 1) 排序键:station_id,submitted_at DESC 排序方式:外部合并磁盘:681040kB ->对station_logs进行Seq扫描(成本= 0.00..598895.16行= 20489916宽度= 20)(实际时间= 0.023..3443.587行= 20489812循环= $ 计划时间:0.072毫秒 执行时间:27690.644 ms
创建索引:
CREATE INDEX station_id__submitted_at ON station_logs(station_id, submitted_at DESC);
创建索引后,对于同一查询:
唯一(费用= 0.56..2156367.51行= 89宽度= 20)(实际时间= 0.184..16263.413行= 98循环= 1) ->使用station_logs上的station_id__submitted_at进行索引扫描(成本= 0.56..2105142.98行= 20489812宽度= 20)(实际时间= 0.181..1 $ 计划时间:0.206毫秒 执行时间:16263.490 ms
有没有办法使此查询更快?像1秒一样,16秒仍然太多。