postgresql 我怎样才能获得最后 10 条记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4131644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the last 10 records
提问by Elitmiar
I have a db with +- 60000 records in, I need to obtain the last 10 records entered, can this be done via postgres? I was thinking off maybe setting the offset to 50 990 and the limit to 10 or something similar, but not sure if this will be efficient?
我有一个包含 +- 60000 条记录的数据库,我需要获取输入的最后 10 条记录,这可以通过 postgres 完成吗?我正在考虑可能将偏移量设置为 50 990 并将限制设置为 10 或类似的值,但不确定这是否有效?
回答by musiKk
Something like the following perhaps:
可能类似于以下内容:
SELECT * FROM your_table
ORDER BY your_timestamp DESC
LIMIT 10
If you want the result sorted by the timestamp, you can wrap this in another query and sort again. You might want to take a look at the execution plan but this shouldn't be too inefficient.
如果您希望按时间戳排序的结果,您可以将其包装在另一个查询中并再次排序。您可能想查看执行计划,但这应该不会太低效。
回答by Yodan Tauber
ORDER BY id DESC LIMIT 10
If id
is indexed, this will be very efficient. Could naturally also be a timestamp.
如果id
被索引,这将非常有效。自然也可以是时间戳。