我使用子选择来选择最新的帖子。这是个好主意吗?
@Query("SELECT p FROM Post p INNER JOIN p.user WHERE p.user.username = :username AND p.updated = (SELECT MAX(p.updated) FROM Post p WHERE p.user.username = :username)")
Post findLatestByUsername(@Param("username") String username);
答案 0 :(得分:-1)
我认为使用索引下降选择最新行的最佳方法
将username, updated
列设置为下降index
如果您没有这些列上的索引,有关如何设置它的更多信息,您可以查看下面的链接
https://www.postgresql.org/docs/9.5/static/sql-createindex.html
然后你的选择就像这样
SELECT * FROM Post p where p.user.username = :username LIMIT 1
你的阅读数据在选择后已经下降了,然后你可以选择1行,它将是最新的行,数据是由username
排序的(并且在where子句中)和{{1列
关于HQL的限制,你可以通过其他方式解决这个问题 请看下面的链接
https://groups.google.com/forum/#!topic/nhibernate-development/JYE3NDcTUEA