EntityFramework.BulkInsert扩展插入数据和EF本身插入数据比较
扩展下载地址:http://efbulkinsert.codeplex.com/
注意同时安装依赖项目,不然会报错,还有,程序中有同一个dll的其他版本,那就可能一次安装不上,得一个一个安装依赖的dll
Install-Package EntityFramework.MappingAPI -Version 6.0.0.7
Install-Package EntityFramework.BulkInsert-ef6
EntityFramework.BulkInsert插入数据和EF比较
初步猜测,它应该只是把多个sql合成一个,不管怎么优化,总该最后生成的是sql。
例如:20条数据,ef调试时看到的是一次连接,20次执行sql,这个批量,估计是一次连接,20个sql组合放到一个字符串提交,这样能减少时间。
再优化也不可能把sql给减少,同一sql在数据库中执行时间也不是EF能减少的。
实测(222数据库,表FinanceReceipts):
用Stopwatch监视执行时间(单位毫秒)
一次插入200条单据测试
EF插入耗时:11,086
BulkInsert插入耗时:740
一次插入10000条单据测试
EF插入耗时:510,640
BulkInsert插入耗时:3,200
通过看代码,和猜测的实现方式差不多,不过,代码中有表映射,为什么有这些功能?
因为 Insert 比数据库自带的 SqlBulkCopy 功能慢,
EntityFramework.BulkInsert扩展在优化语句传输次数的同时,也采用了速度更快的 SqlBulkCopy 去将数据插入数据库,所以在插入大数据量时,比起EF本身的插入数据,可以说快得“离谱”。
不过,利用这个SqlBulkCopy快速插入数据,也就只能在插入上有改进,对于Update,Delete数据,速度上没有什么改进的
//批量插入测试代码
- StringBuilder sb = new StringBuilder();
- FinanceReceipts model = ReceiptsRepository.Entities.Include(o => o.FinanceReceiptDetail).Include(o => o.FinanceBillLog).First(o => o.ReceiptId == 214539);
- int createCount = 10000;
- model.ReceiptId = 0;
- model.ReceiptStatus = -1;
- model.ReceiptNo = "";
- model.FinanceBillLog.OpenSafe().ToList().ForEach(m => m.ReceiptId = 0);
- model.FinanceReceiptDetail.OpenSafe().ToList().ForEach(m => m.ReceiptId = 0);
- model.ActualCreateTime = DateTime.Now;
- List<FinanceReceipts> entities = new List<FinanceReceipts>();
- for (int i = 0; i < createCount; i++)
- {
- FinanceReceipts temp = model.DeepCopy();
- model.ReceiptNo = "ef" + i;
- entities.Add(temp);
- }
- Stopwatch sw = new Stopwatch();
- sw.Start();
- ReceiptsRepository.Insert(entities);
- sw.Stop();
- sb.AppendFormat("EF插入耗时:{0}\r\n", sw.ElapsedMilliseconds);
- model.ActualCreateTime = DateTime.Now;
- List<FinanceReceipts> entities2 = new List<FinanceReceipts>();
- for (int i = 0; i < createCount; i++)
- {
- FinanceReceipts temp = model;
- model.ReceiptNo = "bi" + i;
- entities2.Add(temp);
- }
- sw.Restart();
- var ctx = (this.UnitOfWork as UnitOfWorkContextBase).DbContext;
- using (var transactionScope = new TransactionScope())
- {
-
- ctx.BulkInsert(entities2);
- ctx.SaveChanges();
- transactionScope.Complete();
- }
- sw.Stop();
- sb.AppendFormat("BulkInsert插入耗时:{0}\r\n", sw.ElapsedMilliseconds);
- string ret = sb.ToString();
插入100条,每次插入一条,循环插入测试
第1次:
EF插入耗时:9006
BulkInsert插入耗时:4173
第2次:
EF插入耗时:8738
BulkInsert插入耗时:3806
第3次:
EF插入耗时:8784
BulkInsert插入耗时:3727
BulkInsert还是比EF本身插入数据稍微快一点,总的来说:
BulkInsert 大致是只传一次sql语句,并用SqlBulkCopy快速插入(单据+单据明细+单据日志);
而EF是每个实体一条普通的Insert插入(单据1次,单据明细有多少条就传多少次,单据日志有多少条就传多少次)
//每次插入一条,循环插入的测试代码
- StringBuilder sb = new StringBuilder();
- FinanceReceipts model = ReceiptsRepository.Entities.Include(o => o.FinanceReceiptDetail).Include(o => o.FinanceBillLog).First(o => o.ReceiptId == 214539);
- int createCount = 10;
- model.ReceiptId = 0;
- model.ReceiptStatus = -1;
- model.ReceiptNo = "";
- model.FinanceBillLog.OpenSafe().ToList().ForEach(m => m.ReceiptId = 0);
- model.FinanceReceiptDetail.OpenSafe().ToList().ForEach(m => m.ReceiptId = 0);
- model.ActualCreateTime = DateTime.Now;
- List<FinanceReceipts> entities = new List<FinanceReceipts>();
- for (int i = 0; i < createCount; i++)
- {
- FinanceReceipts temp = model.DeepCopy();
- model.ReceiptNo = "ef" + i;
- entities.Add(temp);
- }
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (int i = 0; i < createCount; i++)
- {
- ReceiptsRepository.Insert(entities[i]);
- }
- sw.Stop();
- sb.AppendFormat("EF插入耗时:{0}\r\n", sw.ElapsedMilliseconds);
- model.ActualCreateTime = DateTime.Now;
- List<FinanceReceipts> entities2 = new List<FinanceReceipts>();
- for (int i = 0; i < createCount; i++)
- {
- FinanceReceipts temp = model;
- model.ReceiptNo = "bi" + i;
- entities2.Add(temp);
- }
- sw.Restart();
- var ctx = (this.UnitOfWork as UnitOfWorkContextBase).DbContext;
- for (int i = 0; i < createCount; i++)
- {
- using (var transactionScope = new TransactionScope())
- {
-
- ctx.BulkInsert(new List<FinanceReceipts>(){entities2[i]});
- ctx.SaveChanges();
- transactionScope.Complete();
- }
- }
- sw.Stop();
- sb.AppendFormat("BulkInsert插入耗时:{0}\r\n", sw.ElapsedMilliseconds);
- string ret = sb.ToString();
附:EntityFramework.BulkInsert 突出点是快,但是实际应用上,有诸多不便,比如:
我们用EF插入,自动增长键会带回数据库中的值,但是BulkInsert 不会;
外键关联表的数据,比如:日志、详情,用EF时,如果有数据会自动插入;用BulkInsert 不会插入,只会插入主表的数据,还要自己根据业务,查询出主表的主键然后赋值给 日志、详情的外键字段,然后我们才可以将这些数据再次插入数据库。
这个是比较麻烦的地方。
最新:
虽然在事务using语句中,但 ctx.BulkInsert(entityArray); 不会回滚事务 ,
加上参数,才能回滚:ctx.BulkInsert(entityArray, tran.UnderlyingTransaction);
from:http://blog.csdn.net/pukuimin1226/article/details/48024995