SubSonic AddMany() 与 foreach 循环 Add()
我试图弄清楚 SubSonics AddMany() 方法是否比简单的 foreach 循环更快。我在 SubSonic 网站上浏览了一下,但没有看到太多关于性能统计数据的信息。
我目前拥有的。 (.ForEach() 只是对其进行了一些验证,除了它的工作原理与 forEach(.....){ do stuff} 一样),
records.ForEach(record =>
{
newRepository.Add(record);
recordsProcessed++;
if (cleanUp) oldRepository.Delete<T>(record);
});
这也会改变
newRepository.AddMany(records);
if (cleanUp) oldRepository.DeleteMany<T>(records);
如果您注意到使用此方法,我会丢失多少条记录的计数我已经处理了这并不重要...但是如果能够向用户显示使用此工具移动了多少记录,那就太好了。
所以我的问题归结为:AddMany() 使用起来会明显更快吗?有什么方法可以计算实际复制的记录数吗?如果成功,我可以假设所有记录都已处理吗?如果一条记录失败,那么整个过程就失败了吗?
提前致谢。
I'm trying to figure out whether or not SubSonics AddMany() method is faster than a simple foreach loop. I poked around a bit on the SubSonic site but didn't see much on performance stats.
What I currently have. (.ForEach() just has some validation it it, other than that it works just like forEach(.....){ do stuff})
records.ForEach(record =>
{
newRepository.Add(record);
recordsProcessed++;
if (cleanUp) oldRepository.Delete<T>(record);
});
Which would change too
newRepository.AddMany(records);
if (cleanUp) oldRepository.DeleteMany<T>(records);
If you notice with this method I lose the count of how many records I've processed which isn't critical... But it would be nice to be able to display to the user how many records were moved with this tool.
So my questions boil down to: Would AddMany() be noticeably faster to use? And is there any way to get a count of the number of records actually copied over? If it succeeds can I assume all the records were processed? If one record fails, does the whole process fail?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
澄清一下,AddMany() 每行生成单独的查询并通过批处理提交它们; DeleteMany() 生成单个查询。当您想知道查询发生了什么时,请查阅源代码和生成的 SQL。
您的第一种方法很慢:2*N 查询。但是,如果您使用批处理提交查询,速度会更快。
第二种方法更快:N+1 查询。您只需通过枚举“记录”即可找到将添加的数量。
如果存在超出批次大小容量限制的风险,则一次提交 50 或 100 个,几乎不会受到惩罚。
您的最后一个问题取决于交易。如果整个操作是一个事务,则将中止作为一个提交。否则,每个查询都将是独立的。你的选择。
Just to clarify, AddMany() generates individual queries per row and submits them via a batch; DeleteMany() generates a single query. Please consult the source code and the generated SQL when you want to know what happens to your queries.
Your first approach is slow: 2*N queries. However, if you submit the queries using a batch it would be faster.
Your second approach is faster: N+1 queries. You can find how many will be added simply by enumerating 'records'.
If there is a risk of exceeding capacity limits on the size of a batch, then submit 50 or 100 at a time with little penalty.
Your final question depends on transactions. If the whole operation is one transaction, it will commit of abort as one. Otherwise, each query will stand alone. Your choice.