I have this postgresql code.
SELECT *
FROM listings
WHERE status != 'inactive'
ORDER BY pkey DESC LIMIT 1000 OFFSET 0
Precisely, this will query the FIRST 1000 listings which is still active then sort it DESC.
Is there a best way to query the most recent/latest data?
Like for example I have a 500000 rows, now when I executed the query I want the 499000-500000 to be queried DESC.
Thanks for the help.
Just to mince words, you said
Which is not accurate. It will sort sort id DESC and then select the “first” 1000 (which, when sorted DESC can be thought of as the “last” 1000).
So, I think the answer to your question is in your question. The query you show will select the “last” (i.e. the records with the greatest values in column ‘pkey`) 1000 records, ordered from the most recent to the one a thousand ago.
Note that the only real caveat is that it’s not exactly 499000-500000, which would only be true if all 1,000 most-recent rows are “active”.