How can I get the last 10 entries

I have a db with + 60,000 records, I need to get the last 10 records, can this be done via postgres? I thought maybe setting the offset to 50,990, and the limit to 10 or something similar, but not sure if this will be effective?

+3
source share
2 answers

Perhaps something like the following:

SELECT * FROM your_table
ORDER BY your_timestamp DESC
LIMIT 10

If you want the result to be sorted by timestamp, you can wrap this in another query and sort it again. You can take a look at the implementation plan, but it should not be too inefficient.

+5
source
ORDER BY id DESC LIMIT 10

If indexed id, it will be very effective. Naturally, this is also a timestamp.

+2
source

Source: https://habr.com/ru/post/1773789/


All Articles