CloudBlobContainer.ListBlobs() - 使用 ToList() 降低交易成本
我有两个代码示例:
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
//Do somthing
}
第二
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
//Do somthing
}
个示例会在“事务方面”带来任何优势吗?
每个示例中对 Blob 存储进行了多少事务?
I have two code examples :
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
//Do somthing
}
and this :
var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
//Do somthing
}
Will the second example give any advantage "transaction-wise" ?
How many transaction are made to the blob storage in each example ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:看来这个问题是参考Martin Ingvar Kofoed Jensen 的博客文章。这个问题和那个问题之间的区别在于对
.Where(...).SingleOrDefault()
的调用。由于 LINQ 是惰性求值的,因此在 Container.ListBlobs() 阶段,它仍然是一个 IEnumerable,尚未调用 REST API(尚未检索到任何数据) )。一旦发生结果操作(例如ToList()
或SingleOrDefault()
),就会下载数据。由于在以下代码中循环内的惰性列表上调用了非惰性操作,因此它会为每个循环迭代产生一个事务:关于实际编写的问题:两个代码片段都会产生 单个事务(最多 5,000 个 blob)。我在 LinqPad/Fiddler,我只看到一个 单个 API 调用 生成 blob 列表(用 {mystore} 替换我们的存储名称):
根据 列出 Blob REST API,一次调用最多可返回 5,000 个结果。因此,如果要列出整个容器中的所有 blob,最多需要 (# blobs / 5000) 个事务。
Edit: It appears this question is in reference to a blog post by Martin Ingvar Kofoed Jensen. The difference between this question and that question is the call to
.Where(...).SingleOrDefault()
. Because LINQ is lazy-evaluated, at the stage ofContainer.ListBlobs()
it is still anIEnumerable
and hasn't yet called the REST API (no data has been retrieved). As soon as a result operation occurs (such asToList()
orSingleOrDefault()
), the data is downloaded. Since a non-lazy operation is called on a lazy list inside the loop in the following code, it incurs a transaction for every loop iteration:Regarding the question as it's actually written: Both code snippets will incur a single transaction (up to 5,000 blobs). I tested both code snippets in LinqPad/Fiddler, and I only see a single API call to generate the list of blobs (replaced our storage name with {mystore}):
According to the documentation for List Blobs REST API, up to 5,000 results can be returned in a single call. So if you want to list all blobs in the entire container, it will take at most (# blobs / 5000) transactions.