小编Fey*_*eyd的帖子

从大表中获取每组最大价值的高效查询

鉴于表:

    Column    |            Type             
 id           | integer                     
 latitude     | numeric(9,6)                
 longitude    | numeric(9,6)                
 speed        | integer                     
 equipment_id | integer                     
 created_at   | timestamp without time zone
Indexes:
    "geoposition_records_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)

该表有 2000 万条记录,相对而言,这不是一个大数目。但它会使顺序扫描变慢。

我怎样才能获得max(created_at)每个的最后一条记录 ( ) equipment_id

我已经尝试了以下两个查询,其中有几个变体,我已经阅读了本主题的许多答案:

select max(created_at),equipment_id from geoposition_records group by equipment_id;

select distinct on (equipment_id) equipment_id,created_at 
  from geoposition_records order by equipment_id, created_at desc;
Run Code Online (Sandbox Code Playgroud)

我也尝试过创建 btree 索引,equipment_id,created_at但 Postgres 发现使用 seqscan 更快。强制enable_seqscan = off也没有用,因为读取索引与 seq 扫描一样慢,可能更糟。

查询必须定期运行,始终返回最后一个。

使用 Postgres …

postgresql performance index greatest-n-per-group

17
推荐指数
2
解决办法
3万
查看次数