如何使用存储库模式和工作单元进行EF批量插入并提供更高的性能? [英] How to use repository pattern and unit of work for bulk insert with EF and give more performance?

查看:117
本文介绍了如何使用存储库模式和工作单元进行EF批量插入并提供更高的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在开发一个项目,使用工作单元和存储库模式与orm EF6。

i想要将大约800k记录的对象列表插入到数据库中。

i每15k发送一次或者10k记录到存储库的实例然后提交它。

它太慢了。我怎样才能提供更多性能?

我不想安装EF.Extensions。

我的问题是关于foreach循环。它太慢了添加到存储库。



我尝试过:



foreach(var imei in command.IMEIItems){



var newIMEI = new IMEI(imei.IMEINumber,command.PromotionId);

_imeiRepository.Add(newIMEI);

}



_unitOfWork.Commit();

Hi. I am working on a project that use unit of work and repository pattern with orm EF6.
i want insert a list of object about 800k record into database.
i send every 15k or 10k record to instance of repository and then commit it.
it is too slow. how can i give more performance?
i dont want to instal EF.Extensions.
my problem is about foreach loop. its too slow add to repository.

What I have tried:

foreach (var imei in command.IMEIItems) {

var newIMEI = new IMEI(imei.IMEINumber, command.PromotionId);
_imeiRepository.Add(newIMEI);
}

_unitOfWork.Commit();

推荐答案

使用普通的香草EF无法真正提高性能。无论你做什么,EF总是会为你要添加的每个项目一次执行一次INSERT。没有办法加快速度。



我建议我在EF之外走一步并使用 SqlBulkCopy [ ^ ]类。你将不得不做更多的工作来设置这个操作,但它肯定比EF快很多。
You can't really improve the performance using plain vanilla EF. No matter what you do, EF is always going to do one INSERT at a time for each item you're adding. There's no way to speed that up.

I would suggest stepping outside EF for this and using the SqlBulkCopy[^] class instead. You're going to have to do a bit more work to setup this operation, but it'll definitely be a lot faster than EF.


Dave说的是真实的和真实的大数据导入值得一试。我只是想注意,我在Repository-Impl上提供了一个AddRange方法,并且只进行了一次DataContext.Save - 所以对于正常用例来说是一种实用的方法 - 顺便说一下。永远不要在没有Pre-JIT的情况下查看调试中的EF性能,或者如果尚未生成EF-Views(您也可以预先生成这些)。所以始终建立发布 - 运行然后再次运行你的性能测量。
What Dave said is kind of true and for real big data import worth a try. I just wanted to notice that I offer an AddRange method on my Repository-Impl and do DataContext.Save only once - so for normal use cases a practical way - Btw. Never look at EF Performance in Debug, without Pre-JIT or if EF-Views are not allready generated (you can pre-generated these too). So Always Build Release - run and then run again an do your performance meassurements.


这篇关于如何使用存储库模式和工作单元进行EF批量插入并提供更高的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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