最大id日期:sql/oracle优化
有什么更优雅的方法可以做到这一点:
select date from table where id in (
select max(id) from table);
当然有更好的方法......
What is a more elegant way of doing this:
select date from table where id in (
select max(id) from table);
Surely there is a better way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ROWNUM
伪列。在找到第一行之前,必须使用子查询来对结果进行排序:您可以使用 子查询在 Oracle 9i 及更高版本中,按以下方式分解:
您可以使用自联接,并查找不存在具有更大 id 的行的位置:
哪种解决方案最好取决于表中的索引以及数据量和数据量。您的数据的分布。您应该测试每个解决方案,以确定哪种解决方案最有效、最快、最灵活地满足您的需求等。
You can use the
ROWNUM
pseudocolumn. The subquery is necessary to order the result before finding the first row:You can use subquery factoring in Oracle 9i and later in the following way:
You can use a self-join, and find where no row exists with a greater id:
Which solution is best depends on the indexes in your table, and the volume and distribution of your data. You should test each solution to determine what works best, is fastest, is most flexible for your needs, etc.
假设你的 id 是唯一的。
编辑:使用子查询+ rownum
assuming your ids are unique.
EDIT: using subquery + rownum