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

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

问题描述

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

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. 在这两个中,我应该指定什么?

  2. 来自 javadoc oracle文档,这是我的地方对何时感到困惑

  1. Of these two, what should I specify?
  2. From javadoc and oracle documentation, this is where I get confused about "when"

Javadoc


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

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

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.

如果我错了,请纠正我。这是否意味着setFetchSize在执行查询之前只是Affective?(因此,ResultSet上的setFetchSize是无用的?但是恰好可以随时更改获取大小?)

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


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

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);




只进,只读结果集的组合,获取大小为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.

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

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

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

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