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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:22:47  来源:igfitidea点击:

How can I get the last 10 records

postgresql

提问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 idis indexed, this will be very efficient. Could naturally also be a timestamp.

如果id被索引,这将非常有效。自然也可以是时间戳。