I have the following query which fetches the id
of the latest N observations
for each station
:
SELECT id
FROM (
SELE
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.