SqlDataReader,一个消防游标 - 没有缓冲?

发布于 2024-08-10 14:54:46 字数 394 浏览 1 评论 0原文

所以我知道 SqlDataReader 非常快,你不知道你会得到多少行,它一次只为每一行分配内存。那么,如果我有一百万行小行(比如 4 到 32 字节),则每行读取意味着到服务器的往返?没有发生缓冲,对吗?

如果我通过 SQL Native Client 使用 ODBC,我可以设置一个适当大的行缓冲区,以便每次往返获取 512 行。当消费者读取第 0 行时,我会获取 512 行,然后返回第 0 行,接下来的 511 次读取是内存访问,第 513 次读取是新的往返。

对我来说,这应该更快(并且不必仅转发),但是这也取决于结果集在服务器上的具体化方式。在后一种情况下,必须先完成结果集,然后才能开始通过网络获取数据,但对于我认为它使用的 SqlDataReader 和 TDS 可能不是这种情况(?),这是我不熟悉的协议。任何意见表示赞赏。

So I understand SqlDataReader is quite fast, you dont know how many rows you'll get and it only allocates memory for each row at a time. So if I have a million rows of small rows (say 4 to 32 bytes) each row read means a roundtrip to the server? No buffering takes place right?

If I used ODBC via SQL Native Client I could setup a row buffer appropriately large to fetch say 512 rows each roundtrip. When the consumer reads row 0 I do a fetch of 512 rows and then return row 0, the next 511 reads are memory accesses and the 513th read is a new roundtrip.

To me this should be faster (and wouldnt have to be forward only), however it depends on how the result set is materialized on the server also. In the latter case the result set would have to be finished before I can start fetching over the network, but may not be the case (?) for SqlDataReader and TDS which I believe it uses, a protocol I'm not familiar with. Any comments appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2024-08-17 14:54:46

DataReader 通过其 api 一次仅分发 1 行,但它会从数据库批量获取数据,缓冲数据,因此不必为每一行往返数据库。

a DataReader hands out just 1 row at a time via its api, but it bulk fetches from the database, buffering up data so it doesn't havee to make a round trip to the db for each row.

过度放纵 2024-08-17 14:54:46

不,SqlDataReader 在客户端进行缓冲。它绝对不会往返服务器来获取您的行。

No, SqlDataReader does buffer on the client side. It is definitely not doing a round trip to the server to fetch your rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文