.NET批量大数据插入性能分析及比较(5.使用SqlBulkCopy)

来源:互联网 发布:数据 战略资源 编辑:程序博客网 时间:2024/06/04 00:21

[c-sharp] view plaincopy
  1. #region 使用SqlBulkCopy  
  2.         public static bool ExecuteTransactionScopeInsert(DataTable dt, int batchSize)  
  3.         {  
  4.             int count = dt.Rows.Count;  
  5.             string tableName = "TestTable";  
  6.             int copyTimeout = 600;  
  7.             bool flag = false;  
  8.             try  
  9.             {  
  10.                 using (SqlConnection cn = new SqlConnection(connectionString))  
  11.                 {  
  12.                     using (TransactionScope scope = new TransactionScope())  
  13.                     {  
  14.                         cn.Open();  
  15.                         using (SqlBulkCopy sbc = new SqlBulkCopy(cn))  
  16.                         {  
  17.                             //服务器上目标表的名称     
  18.                             sbc.DestinationTableName = tableName;  
  19.                             sbc.BatchSize = batchSize;  
  20.                             sbc.BulkCopyTimeout = copyTimeout;  
  21.                             for (int i = 0; i < dt.Columns.Count; i++)  
  22.                             {  
  23.                                 //列映射定义数据源中的列和目标表中的列之间的关系     
  24.                                 sbc.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);  
  25.                             }  
  26.                             sbc.WriteToServer(dt);  
  27.                             flag = true;  
  28.                             scope.Complete();//有效的事务     
  29.                         }  
  30.                     }  
  31.                 }  
  32.             }  
  33.             catch (Exception ex)  
  34.             {  
  35.                 LogHelper.Error(ex.Message);  
  36.                 return false;  
  37.             }  
  38.             return flag;  
  39.         }  
  40.         #endregion  

 

结果如下:

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:10;Time:10314;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:20;Time:4476;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:50;Time:2021;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:100;Time:1332;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:200;Time:978;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:400;Time:730;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:500;Time:649;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:600;Time:623;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:700;Time:669;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:800;Time:585;

Use SqlServer TransactionScope Insert;RecordCount:40000;BatchSize:1000;Time:681;

 

SqlBulkCopy原理是采用了SQL Server的BCP协议进行数据的批量复制,结合使用事务,就我们的案例而言,大约每批800条是平衡点,性能比逐条插入提高了100多倍,并前面同样使用事务批量插入的案例性能提升了7倍以上。

 

0 0