我应该在什么时候指定 setFetchSize()? [英] What and when should I specify setFetchSize()?

查看:32
本文介绍了我应该在什么时候指定 setFetchSize()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多 JDBC/MySQL 的最佳实践"指南告诉我要指定 setFetchSize().

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

Statement.setFetchSize() 或 PreparedStatement.setFetchSize()结果集.setFetchSize()

  1. 在这两个中,我应该指定什么?
  2. 来自 javadocoracle 文档,这就是我对何时"感到困惑的地方

Javadoc

<块引用>

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

甲骨文文档

<块引用>

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

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

解决方案

你应该阅读这个 page 来自关于结果集的官方文档.它说

<块引用>

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

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

<块引用>

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

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

I see a lot of "best practices" guides for JDBC/MySQL that tells me to specify 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 javadoc and oracle documentation, this is where I get confused about "when"

Javadoc

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

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"?)

解决方案

You should read this page from 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.

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.

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

这篇关于我应该在什么时候指定 setFetchSize()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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