提高实体框架中的批量插入性能 [英] Improving bulk insert performance in Entity framework

查看:116
本文介绍了提高实体框架中的批量插入性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过实体框架在表中插入20000条记录,大约需要2分钟。除了使用SP以提高其性能之外,是否有其他方式。这是我的代码:

  foreach(Employees项目顺序)
{
t = new Employees ;
t.Text = item.Text;
dataContext.Employees.AddObject(t);
}
dataContext.SaveChanges();


解决方案

有几个改进的机会(如果你正在使用 DbContext ):



设置:

  yourContext.Configuration.AutoDetectChangesEnabled = false; 
yourContext.Configuration.ValidateOnSaveEnabled = false;

在100的包中执行 SaveChanges()插入...或者您可以尝试使用1000个项目的包,并查看性能的更改。



由于在所有这些插入过程中,上下文是相同的,它越来越大,您可以每1000个插入重建上下文对象。 var yourContext = new YourContext(); 我认为这是很大的收获。



在导入中进行这种改进我的数据处理,从7分钟到6秒。



实际的数字...在你的情况下不能是100或1000 ...尝试并调整它。


I want to insert 20000 records in a table by entity framework and it takes about 2 min. Is there any way other than using SP to improve its performance. This is my code:

 foreach (Employees item in sequence)
 {
   t = new Employees ();
   t.Text = item.Text;
   dataContext.Employees.AddObject(t);                  
 }
 dataContext.SaveChanges();

解决方案

There is opportunity for several improvements (if you are using DbContext):

Set:

yourContext.Configuration.AutoDetectChangesEnabled = false;
yourContext.Configuration.ValidateOnSaveEnabled = false;

Do SaveChanges() in packages of 100 inserts... or you can try with packages of 1000 items and see the changes in performance.

Since during all this inserts, the context is the same and it is getting bigger, you can rebuild your context object every 1000 inserts. var yourContext = new YourContext(); I think this is the big gain.

Doing this improvements in an importing data process of mine, took it from 7 minutes to 6 seconds.

The actual numbers... could not be 100 or 1000 in your case... try it and tweak it.

这篇关于提高实体框架中的批量插入性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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