什么是缓存参数小巧玲珑点网呢? [英] What does the buffered parameter do in Dapper dot net?

查看:239
本文介绍了什么是缓存参数小巧玲珑点网呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小巧玲珑点网有一个缓存参数(布尔),但据我可以告诉的只有的东西确实是投在返回之前产生一个列表。

由于每<一href="http://$c$c.google.com/p/dapper-dot-net/#Buffered_vs_Unbuffered_readers">documentation:

  

精致小巧的默认行为是执行SQL和缓冲整个   读者的回报。这是理想的,在大多数情况下,因为它最大限度地减少共享   在db锁,并减少了分贝网的时间。

     

但在执行庞大的查询时,您可能需要减少内存   根据需要足迹,只加载对象。要做到这一点通过,缓冲:   假到查询方法。

我不知道如何将结果转换列表实现这一目的。我失去了一些东西?我唯一​​的想法是,它应该设置的CommandBehavior 的ExecuteReader 的CommandBehavior。 SequentialAccess (但事实并非如此)。

感谢您启发了我。

解决方案
  

但据我可以返回之前告诉它的作用是把结果以列表的唯一的事

您不会遗漏任何东西。这是关键的区别。除了它不是一个的的为这样:实际返回的对象是非常不同的。基本上,有读取数据的方式有两种:

  • 在一个流API的每个元素都单独产生;这是非常内存使用效率,但是如果你做大量的每个项目的后续处理,意味着你的连接/命令可以是积极的较长一段时间
  • 在缓冲API的所有行读取的的事情是产生

如果你正在阅读一个非常大的数据量(十万到数百万行),非缓冲的API可以是preferable。否则大量内存使用,且有可能会出现明显延迟甚至在第一行可用之前。然而,在最常见的情况读出的数据的量是在合理范围内,所以可以合理地将其推入一个列表递给呼叫者之前。这意味着,命令/读卡器等已完成的之前返回。

作为一个侧面说明,缓冲模式也避免了哦,所以,常见的已经有连接在一个开放的阅读器(或任何确切的措辞是)。

Dapper dot net has a buffer parameter (a bool), but as far as I can tell the only thing it does is cast the result to a list before returning it.

As per the documentation:

Dapper's default behavior is to execute your sql and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time.

However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method.

I'm not sure how casting the result to a list accomplishes this. Am I missing something? My only idea is that it is supposed to set the CommandBehavior for the ExecuteReader to CommandBehavior.SequentialAccess (but it doesn't).

Thanks for enlightening me.

解决方案

but as far as I can tell the only thing it does is cast the result to a list before returning it

You aren't missing anything. That is the key difference. Except it isn't a cast as such: the actual returned object is very different. Basically, there are two ways of reading data:

  • in a streaming API each element is yielded individually; this is very memory efficient, but if you do lots of subsequent processing per item, mean that your connection / command could be "active" for an extended time
  • in a buffered API all the rows are read before anything is yielded

If you are reading a very large amount of data (thousands to millions of rows), a non-buffered API may be preferable. Otherwise lots of memory is used, and there may be noticeable latency before even the first row is available. However, in most common scenarios the amount of data read is within reasonable limits, so it is reasonable to push it into a list before handing it to the caller. That means that the command / reader etc has completed before it returns.

As a side note, buffered mode also avoids the oh-so-common "there is already an open reader on the connection" (or whatever the exact phrasing is).

这篇关于什么是缓存参数小巧玲珑点网呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆