JDBC Statement.setFetchsize 是如何工作的

How JDBC Statement.setFetchsize exactly works

我想在我的自定义 JDBC 驱动程序中实现 this 方法功能。我应该使用 JDBC 中的哪个方法来设置 MaxResults?

如果我的查询结果是1000行,并且设置了setFetchSize=100的值,那么结果会是什么?它将 return 只有 100 行,或者在 return 前 100 行之后,它将从数据库中获取接下来的 100 行,直到完成 1000 行。

来自Statement.setFetchSize

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects generated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.

因此,如果您总共有 1000 行并且提取大小为 100(假设驱动程序不忽略提示或提取近似 nr),那么这意味着驱动程序将提取 10 行中的所有行每批 100 行。当本地没有可用的行来完成对 next() 的调用时,将提取一批行,直到提取完所有行或达到 maxRows

结果如下:

Statement stmt = ...;
stmt.setFetchSize(100);
ResultSet rs = ...;
rs.next(); // Triggers fetch of 100 rows, positions on row 1 / 100
rs.next(); // Positions on row 2 / 100
// 3-99 rs.next();
rs.next(); // Positions on row 100 / 100
rs.next(); // Triggers fetch of 100 rows, positions on row 101 / 200
// etc

这是从JDBC规范的角度来说比较理想的。有些驱动程序会忽略提示(例如 MySQL 滥用它来逐行获取,如果您将其设置为 Integer.MIN_VALUE 并忽略所有其他值),并且一些数据库会接受提示,但是 return 比请求的行少(有时多)。

我发现 com.google.api.services.bigquery.model.QueryRequest.setMaxResults 你的文档 link 不完全清楚,但它似乎做同样的事情。