JDBC 结果集 SQL 服务器

JDBC ResultSet with SQL Server

当使用 JDBC createStatement 查询并获取数百万行的结果集(来自 SQL 服务器)时,真正执行的 SQL 查询是什么,这些行存储在哪里以及如何存储许多行?它使用 SQL 服务器游标吗? 我知道客户端内存中只保留了有限数量的行(Java 代码),所以其余的行保存在哪里,或者它们可能还没有从 SQL服务器)? 当我查看 SQL 服务器分析器时,它只显示正常查询(它没有添加游标或其他内容的 TOP X)

取自 SQL Server documentation on Adaptive Buffering:

Normally, when the Microsoft JDBC Driver for SQL Server executes a query, the driver retrieves all of the results from the server into application memory. Although this approach minimizes resource consumption on the SQL Server, it can throw an OutOfMemoryError in the JDBC application for the queries that produce very large results.

有一种解决方法,SQL 服务器调用 Adaptive Buffering 并且似乎默认为 JDBC driver version 2+ 启用,您可能想在文档中阅读它.这是要点:

In order to allow applications to handle very large results, the Microsoft JDBC Driver for SQL Server provides adaptive buffering. With adaptive buffering, the driver retrieves statement execution results from the SQL Server as the application needs them, rather than all at once.