简体   繁体   English

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

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

Can I see how many times a Java ResultSet is asking for data from the database?我可以看到 Java ResultSet 从数据库中请求数据的次数吗? I have a big table (1 million records) and I want to see how many blocks of data it's pulling from the database.我有一个大表(100 万条记录),我想看看它从数据库中提取了多少数据块。 My database is Oracle.我的数据库是 Oracle。

The "Result Set" documentation states: “结果集”文档指出:

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.默认情况下,当 Oracle JDBC 运行查询时,它会从数据库 cursor 中一次检索 10 行的结果集。这是默认的 Oracle 行提取大小值。 You can change the number of rows retrieved with each trip to the database cursor by changing the row fetch size value.您可以通过更改行提取大小值来更改每次访问数据库 cursor 时检索到的行数。

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.标准 JDBC 还允许您指定每次数据库往返查询所提取的行数,该数字称为提取大小。 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.在 Oracle JDBC 中,行预取值用作语句 object 中的默认提取大小。设置提取大小会覆盖行预取设置,并影响通过该语句运行的后续查询 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.当语句 object 运行查询时,语句 object 的提取大小将传递给查询产生的结果集 object。 However, you can also set the fetch size in the result set object to override the statement fetch size that was passed to it.但是,您也可以在结果集中设置提取大小 object 以覆盖传递给它的语句提取大小。

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.生成结果集后对语句 object 的提取大小所做的更改不会影响该结果集。

The result set fetch size, either set explicitly, or by default equal to the statement fetch size that was passed to it, determines the number of rows that are retrieved in any subsequent trips to the database for that result set.结果集提取大小(显式设置或默认等于传递给它的语句提取大小)决定了在该结果集的任何后续数据库访问中检索的行数。 This includes any trips that are still required to complete the original query, as well as any refetching of data into the result set.这包括完成原始查询仍然需要的任何行程,以及将数据重新提取到结果集中的任何行程。 Data can be refetched, either explicitly or implicitly, to update a scroll-sensitive or scroll-insensitive/updatable result set.可以显式或隐式地重新获取数据,以更新滚动敏感或滚动不敏感/可更新的结果集。

Setting the Fetch Size设置获取大小

The following methods are available in all Statement , PreparedStatement , CallableStatement , and ResultSet objects for setting and getting the fetch size:以下方法在所有StatementPreparedStatementCallableStatementResultSet对象中都可用,用于设置和获取提取大小:

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

To set the fetch size for a query, call setFetchSize on the statement object prior to running the query.要设置查询的提取大小,请在运行查询之前对语句 object 调用setFetchSize If you set the fetch size to N , then N rows are fetched with each trip to the database.如果将提取大小设置为N ,则每次访问数据库都会提取N行。

After you have run the query, you can call setFetchSize on the result set object to override the statement object fetch size that was passed to it.运行查询后,您可以对结果集 object 调用setFetchSize以覆盖传递给它的语句 object 提取大小。 This will affect any subsequent trips to the database to get more rows for the original query, as well as affecting any later refetching of rows.这将影响任何后续访问数据库以获取原始查询的更多行,以及影响任何以后的行重新获取。

If you want to know the number of trips to the database then call getFetchSize() on the statement and then divide the total number of rows in the result set by the fetch size (and round up).如果您想知道访问数据库的次数,请在语句中调用getFetchSize() ,然后将结果集中的总行数除以获取大小(并向上舍入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM