有一张数据表,结构: CREATE TABLE target_position ( target_id varchar(80), time bigint, content text );
数据量(单表)是 20-100 亿条 target_id 大约 20 万个
数据库使用的是 PostgreSQL
需求: 查询每个目标指定时间段的最新一条数据。
现在是使用窗口函数来实现,如下: select target_id,time,content from (select *,row_number() over (partition by target_id order by time desc) rid from target_position where time>开始时间 and time<=结束时间) as t where rid=1;