Java 我应该在什么时候指定 setFetchSize()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20899977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:07:45  来源:igfitidea点击:

What and when should I specify setFetchSize()?

javamysqlsqloraclejdbc

提问by Jee Seok Yoon

I see a lot of "best practices" guides for JDBC/MySQL that tells me to specify setFetchSize().

我看到很多 JDBC/MySQL 的“最佳实践”指南告诉我指定 setFetchSize()。

However, I have no idea when to specify, and what(statement, result set) to specify.

但是,我不知道何时指定,以及指定什么(语句,结果集)。

Statement.setFetchSize() or PreparedStatement.setFetchSize() 
ResultSet.setFetchSize()
  1. Of these two, what should I specify?
  2. From javadocand oracle documentation, this is where I get confused about "when"
  1. 在这两个中,我应该指定什么?
  2. javadocoracle 文档中,这就是我对“何时”感到困惑的地方

Javadoc

Javadoc

The default value is set by the Statement object that created the result set. The fetch size may be changed at any time.

默认值由创建结果集的 Statement 对象设置。提取大小可以随时更改。

Oracle Doc

甲骨文文档

Changes made to the fetch size of a statement object after a result set is produced will have no affect on that result set.

生成结果集后对语句对象的提取大小所做的更改不会影响该结果集。

Please correct me if I am wrong. Does this mean that setFetchSize is only Affective before a query is executed?(Therefore, setFetchSize on a ResultSet is useless? But happens to "The fetch size may be changed at any time"?)

如果我错了,请纠正我。这是否意味着 setFetchSize 仅在执行查询之前有效?(因此,ResultSet 上的 setFetchSize 没有用?但碰巧“获取大小可能随时更改”?)

采纳答案by Andreas Wederbrand

You should read this pagefrom the official docs on result sets. It says

您应该从有关结果集的官方文档中阅读此页面。它说

By default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate, and due to the design of the MySQL network protocol is easier to implement. If you are working with ResultSets that have a large number of rows or large values, and cannot allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

默认情况下,ResultSet 被完全检索并存储在内存中。在大多数情况下,这是最有效的操作方式,并且由于 MySQL 网络协议的设计更容易实现。如果您正在使用具有大量行或大值的 ResultSet,并且无法在 JVM 中为所需内存分配堆空间,则可以告诉驱动程序一次将结果流回一行。

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                            java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);

The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. After this, any result sets created with the statement will be retrieved row-by-row.

只进、只读结果集与 Integer.MIN_VALUE 提取大小的组合用作驱动程序逐行流式传输结果集的信号。此后,将逐行检索使用该语句创建的任何结果集。

In effect, setting only fetchSize have no effect on the connector-j implementation.

实际上,仅设置 fetchSize 对 connector-j 实现没有影响。

回答by akasha

Found a good article with examplesto your question:

找到了一篇很好的文章,其中包含您的问题的示例

https://www.baeldung.com/jdbc-resultset

https://www.baeldung.com/jdbc-resultset

7.1. Using Fetch Size on Statement 7.2. Using Fetch Size on ResultSet

7.1. 在 Statement 7.2 上使用 Fetch Size。在 ResultSet 上使用 Fetch Size