我想实现 this我的自定义 JDBC 驱动程序中的方法功能。我应该使用 JDBC 中的哪个方法来设置 MaxResults?
如果我的查询结果是1000行,我设置了setFetchSize=100的值,那么结果会怎样呢?它将仅返回 100 行,或者在返回前 100 行后,它将从数据库中获取接下来的 100 行,直到完成 1000 行。
最佳答案
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 thisStatement
. 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规范的角度来说比较理想。有些驱动程序会忽略提示(例如,如果您将其设置为 Integer.MIN_VALUE
并忽略所有其他值,MySQL 会滥用它逐行获取),并且一些数据库会接受提示,但随后返回比请求的行少(有时多)。
我发现您链接的 com.google.api.services.bigquery.model.QueryRequest.setMaxResults
的文档并不完全清楚,但它似乎做同样的事情。
关于java - JDBC Statement.setFetchsize 是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431823/