Java ResultSet 从数据库中请求数据多少次?

How many times does a Java ResultSet ask for data from database?

我可以看到 Java 结果集从数据库中请求数据的次数吗? 我有一个很大的 table(100 万条记录),我想看看它从数据库中提取了多少数据块。我的数据库是 Oracle。

"Result Set" documentation 状态:

Fetch Size

By default, when Oracle JDBC runs a query, it retrieves a result set of 10 rows at a time from the database cursor. This is the default Oracle row fetch size value. You can change the number of rows retrieved with each trip to the database cursor by changing the row fetch size value.

Standard JDBC also enables you to specify the number of rows fetched with each database round-trip for a query, and this number is referred to as the fetch size. In Oracle JDBC, the row-prefetch value is used as the default fetch size in a statement object. Setting the fetch size overrides the row-prefetch setting and affects subsequent queries run through that statement object.

Fetch size is also used in a result set. When the statement object run a query, the fetch size of the statement object is passed to the result set object produced by the query. However, you can also set the fetch size in the result set object to override the statement fetch size that was passed to it.

Note:

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

结果集提取大小(显式设置或默认等于传递给它的语句提取大小)决定了在该结果集的任何后续数据库访问中检索的行数。这包括完成原始查询仍然需要的任何行程,以及将数据重新提取到结果集中的任何行程。可以显式或隐式地重新获取数据以更新 scroll-sensitive 或 scroll-insensitive/updatable 结果集。

设置获取大小

以下方法在所有 StatementPreparedStatementCallableStatementResultSet 对象中都可用,用于设置和获取提取大小:

void setFetchSize(int rows) throws SQLException
int getFetchSize() throws SQLException

要设置查询的提取大小,请在 运行 执行查询之前对语句对象调用 setFetchSize。如果将提取大小设置为 N,则每次访问数据库都会提取 N 行。

获得 运行 查询后,您可以对结果集对象调用 setFetchSize 以覆盖传递给它的语句对象提取大小。这将影响任何后续访问数据库以获取原始查询的更多行,以及影响任何以后的行重新获取。

如果您想知道访问数据库的次数,请在语句上调用 getFetchSize(),然后将结果集中的总行数除以获取大小(并向上取整)。