JDBC:LIMIT vs setmaxrows(Resultset) [英] JDBC: LIMIT vs setmaxrows(Resultset)

查看:765
本文介绍了JDBC:LIMIT vs setmaxrows(Resultset)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择前10个记录,我可以使用以下其中一个:

I want to select top 10 records, I can use one of:


  • 在Resultset中使用setmaxrows

  • 在sql语句中使用LIMIT和OFFSET

我的问题是:它们之间有什么优缺点?

My question is: what is the advantages and disadvantages beetween them?

推荐答案

对于大多数情况,您希望使用 LIMIT 子句,但在一天结束都会实现你想要的。这个答案针对JDBC和PostgreSQL,但适用于使用类似模型的其他语言和数据库。

For most cases, you want to use the LIMIT clause, but at the end of the day both will achieve what you want. This answer is targeted at JDBC and PostgreSQL, but is applicable to other languages and databases that use a similar model.

Statement.setMaxRows


如果超出限制,多余的行将被静默删除。

If the limit is exceeded, the excess rows are silently dropped.

ie数据库服务器可能返回更多行,但客户端将忽略它们。 PostgreSQL JDBC驱动程序限制了客户端和服务器端。对于客户端,请查看 AbstractJdbc2ResultSet 中的 maxRows 。对于服务器端,请查看 maxRows in QueryExecutorImpl

i.e. The database server may return more rows but the client will just ignore them. The PostgreSQL JDBC driver limits on both the client and server side. For the client side, have a look at the usage of maxRows in the AbstractJdbc2ResultSet. For the server side, have a look of maxRows in QueryExecutorImpl.

服务器方面, PostgreSQL LIMIT 文档说:

Server side, the PostgreSQL LIMIT documentation says:


查询优化器在生成查询
计划时将LIMIT考虑在内

The query optimizer takes LIMIT into account when generating a query plan

因此,只要查询合理,它就只会加载完成查询所需的数据。

So as long as the query is sensible, it will load only the data it needs to fulfill the query.

这篇关于JDBC:LIMIT vs setmaxrows(Resultset)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆