简体   繁体   English

关于mysql中的Query语句

[英]About Query statement in mysql

I use this code for paging in mysql (working with Struts 2 + Hibernate): 我在mysql中使用此代码进行分页(使用Struts 2 + Hibernate):

Query query=getSession().createQuery("from GovOffice");
query.setFirstResult(0);
query.setMaxResult(100);
List<GovOffice> list=query.list();

This will return list of GovOffice which is started from the first record, display 100 records per page. 这将返回从第一条记录开始的GovOffice列表,每页显示100条记录。

Suppose i have 100,000 records, is this query "from GovOffice" get all 100,000 records first? 假设我有100,000条记录,这个“来自GovOffice”的查询是否首先获得所有100,000条记录? By setFirstResult and setMaxReSult it will limit from 100,000 to 100 records.If this is true that means paging must be useless. 通过setFirstResult和setMaxReSult,它将限制从100,000到100个记录。如果这是真的,那意味着分页必须是无用的。

If not is there any way to prove that query will not get data from DB until we call query.list() . 如果没有,有任何方法可以证明在我们调用query.list()之前查询不会从DB获取数据。

Query 's setFirstResult and setMaxResult use the underlying possibilities of the databases. QuerysetFirstResultsetMaxResult使用数据库的基本可能性。

In the case of MySQL, it's limit . 在MySQL的情况下,它是限制

So, no, it doesn't fetch all records and yes it's efficient. 所以,不,它不会获取所有记录,是的,它是有效的。

The proof in the source code ( org.hibernate.dialect.MySQLDialect ): 源代码中的证明( org.hibernate.dialect.MySQLDialect ):

238 public String getLimitString(String sql, boolean hasOffset) {
239     return new StringBuffer( sql.length()+20 )
240         .append(sql)
241         .append( hasOffset ? " limit ?, ?" : " limit ?")
242         .toString();
243 }

I think the documentation makes it pretty clear that only the max results will be retrieved from the database. 我认为文档非常清楚,只会从数据库中检索最大结果。

setMaxResults(int) setMaxResults(INT)

Seems to me to be the equiv of 在我看来是等同的

SELECT * FROM GovOffice LIMIT 0, 100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM