java - 批量读取 SQL 数据库

标签 java sql sql-server-2008 jdbc

我正在使用 JavaSQL RDBMS 中读取并将结果返回给用户。问题是数据库表有 1.55 亿行,这使得等待时间非常长。

我想知道是否可以从数据库中检索结果并将它们递增地(分批)呈现给用户。

我的查询是一个简单的SELECT * FROM Table_Name 查询

是否有一种机制或技术可以分批给我数据库记录的回调,直到 SELECT 查询完成?

使用的 RDBMS 是 MS SQL Server 2008。

提前致谢。

最佳答案

方法 Statement#setFetchSizeStatement#getMoreResults 应该允许您管理从数据库中的增量提取。不幸的是,这是接口(interface)规范,供应商可能会也可能不会实现这些。获取期间的内存管理实际上取决于供应商(这就是为什么我不会严格地说“JDBC 就是这样工作的”)。

来自JDBC documentation on Statement :

setFetchSize(int rows)

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 genrated by this Statement.

getMoreResults()

Moves to this Statement object's next result, returns true if it is a ResultSet object, and implicitly closes any current ResultSet object(s) obtained with the method getResultSet.

getMoreResults(int current)

Moves to this Statement object's next result, deals with any current ResultSet object(s) according to the instructions specified by the given flag, and returns true if the next result is a ResultSet object. current param indicates Keep or close current ResultSet?

此外,这 SO response关于在 SQLServer 2005 中使用 setFetchSize 的答案,以及它似乎如何不管理批量提取。建议使用 2008 驱动程序或此外,使用 jTDS 驱动程序(它在评论中得到赞许)

response to the same SO post也可能有用,因为它包含指向 MSDN 上的 SQLServer 驱动程序设置的链接。

关于 the MS technet website 也有一些有用的信息但更多地与 SQLServer 2005 相关。在我粗略的审查中找不到 2008 特定版本。无论如何,它建议创建声明:

com.microsoft.sqlserver.jdbc.SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY (2004) scrollability for forward-only, read-only access, and then use the setFetchSize method to tune performance

关于java - 批量读取 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716275/

相关文章:

sql - 如何在 Entity Framework 查询中连接字符串?

MySQL - 选择 JSON 键对值的数据

c# - 从 SQL Server 中选择百万条记录

sql - 如何构建查询以仅提供与 T-SQL 中 CSV ID 列表中的所有值匹配的行

sql server 将日期时间转换为另一个时区?

php - 如何使用 ORACLE(OCI) 在 SQL/PHP 中执行更新查询

java - JComboBox 有容量限制吗

java - 在实现的方法上获取 java.lang.AbstractMethodError

java - 从二进制数据中提取某些位

支持获取、设置和删除某些索引的 java arraylist 的 C# 等价物