JDBC与SQL Server的结果集

huangapple go评论88阅读模式
英文:

JDBC ResultSet with SQL Server

问题

使用JDBC的createStatement查询并获取数百万行的结果集(来自SQL Server)时,实际执行了哪些SQL查询?行存储在哪里?有多少行?它使用SQL Server游标吗?
我知道客户端内存(即Java代码所在的地方)中只保留了有限数量的行,那么其余的行存储在哪里?或者它们可能还没有从SQL Server查询出来?
当我查看SQL Server分析器时,只显示了普通查询(没有添加游标或TOP X之类的内容)。

英文:

When query using JDBC createStatement and getting ResultSet of millions of rows (from SQL Server), what SQL queries are really executed, where do the rows are stored and how many rows? Does it use SQL Server cursor?
I know that only a limited number of rows are kept in the client-side memory (where the Java code), so where are the rest of the rows kept, or maybe they are not yet queried from the SQL Server)?
When I looked at SQL Server profiler, it just shows the normal query (it didn't add cursor or TOP X of something else)

答案1

得分: 1

摘自SQL Server自适应缓冲区文档

通常情况下,当Microsoft SQL Server的JDBC驱动执行查询时,驱动程序会将所有结果从服务器检索到应用程序内存中。虽然这种方法最大程度上减少了SQL Server上的资源消耗,但对于生成非常大结果的查询,它可能会在JDBC应用程序中引发OutOfMemoryError。

有一种方法可以解决这个问题,SQL Server称之为自适应缓冲区,在JDBC驱动程序版本2+中似乎默认启用,您可能希望在文档中详细了解它。以下是要点:

为了允许应用程序处理非常大的结果,Microsoft SQL Server的JDBC驱动程序提供了自适应缓冲区。通过自适应缓冲,驱动程序会根据应用程序的需要从SQL Server中检索语句执行结果,而不是一次性全部检索。

英文:

Taken from the 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.

There's a way around that, what SQL Server calls Adaptive Buffering and which seems to be enabled by default for JDBC driver version 2+ and you might want to read up on it in the documentation. Here's the gist:

> 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.

huangapple
  • 本文由 发表于 2020年4月11日 13:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61152604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定