Query last N related rows per row

前端 未结 2 735
长情又很酷
长情又很酷 2020-12-10 21:31

I have the following query which fetches the id of the latest N observations for each station:

SELECT id
FROM (
  SELE         


        
2条回答
  •  时光取名叫无心
    2020-12-10 22:23

    This is a good anwser only if you are not required to query up-to-date live data.

    Preparation (requires postgresql 9.3)

    drop materialized view test;
    create materialized view test as select * from (
      SELECT station_id, id, created_at,
          row_number() OVER(
              PARTITION BY station_id
              ORDER BY created_at DESC
          ) as rn
      FROM (
          SELECT
              station_id,
              id,
              created_at
          FROM observations
      ) s
     ) q WHERE q.rn <= 100 -- use a value that will be your max limit number for further queries
    ORDER BY station_id, rn DESC ;
    
    
    create index idx_test on test(station_id,rn,created_at);
    

    How to query data:

    select * from test where rn<10 order by station_id,created_at;
    

    Your original query was 281 ms on my machine and this new was 15 ms.

    How to update the view with fresh data:

    refresh materialized view test;
    

    I have another solution that does not require materialized view and works with live, up-to-date data. But given that you don't need up-to-date data, this materialized view is much more efficient.

提交回复
热议问题