Hibernate中的setMaxResults(N)如何工作? [英] How does setMaxResults(N) in Hibernate work?
问题描述
setMaxResults
以下简单的场景。
如果我有一个返回100行的查询,并且我将1传递给 setMaxResults
,这会影响返回(如果运行一个 select top 1
语句)或者Hibernate是否首先获得所有结果(在这种情况下全部为100行),然后选择第一个结果本身?
我想问的原因是,当行数开始增加时,它会有一个巨大的性能问题。
谢谢。
Hibernate将为支持限制查询的所有方言生成限制类型查询。由于SQLServerDialect支持此功能(请参阅 org.hibernate.dialect.SQLServerDialect.supportsLimit()
和 .getLimitString()
),你会得到一个 select top 1
-query。
如果您想绝对确定,可以打开调试日志记录或启用 showSql
- 选项和测试。
I am using MS SQL server 2008 with Hibernate. the question I have is how Hibernate implements setMaxResults
Take the following simple scenario.
If I have a query that returns 100 rows and if I pass 1 to setMaxResults
, will this affect the returned result from the SQL server itself(as if running a select top 1
statement) or does Hibernate get all the results first (all 100 rows in this case) and pick the top one itself?
Reason I am asking is that it would have a huge performance issue when the number of rows starts to grow.
Thank you.
Hibernate will generate a limit-type query, for all dialects which supports limit query. As the SQLServerDialect supports this (see org.hibernate.dialect.SQLServerDialect.supportsLimit()
, and .getLimitString()
), you will get a select top 1
-query.
If you would like to be absolutly sure, you may turn on debug-logging, or enable the showSql
-option and test.
这篇关于Hibernate中的setMaxResults(N)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!