.NET批量大数据插入性能分析及比较(2.普通插入与拼接sql批量插入)

首先自然是遍历DataTable逐条数据插入

 

[c-sharp]  view plain copy
  1. public class DbOperation  
  2. {  
  3.     private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectToSql"].ConnectionString;  
  4.     private static string asyncconnectionString = ConfigurationManager.ConnectionStrings["ConnectToSqlAsync"].ConnectionString;  
  5.  
  6.     #region 逐条数据插入  
  7.     public static bool ExecuteInsert(DataTable dt, int batchSize)  
  8.     {  
  9.         int count = dt.Rows.Count;  
  10.         bool flag = false;  
  11.         try  
  12.         {  
  13.             using (SqlConnection cn = new SqlConnection(connectionString))  
  14.             {  
  15.                 using (SqlCommand cmd = new SqlCommand("Insert into TestTable(Id, Name) Values(@Id, @Name)", cn))  
  16.                 {  
  17.                     cn.Open();  
  18.                     for (int i = 0; i < count; i++)  
  19.                     {  
  20.                         cmd.Parameters.AddWithValue("@Id", dt.Rows[i]["Id"]);  
  21.                         cmd.Parameters.AddWithValue("@Name", dt.Rows[i]["Name"]);  
  22.                         cmd.ExecuteNonQuery();  
  23.                         cmd.Parameters.Clear();  
  24.                     }  
  25.                     flag = true;  
  26.                 }  
  27.             }  
  28.         }  
  29.         catch (Exception ex)  
  30.         {  
  31.             LogHelper.Error(ex.Message);  
  32.             return false;  
  33.         }  
  34.         return flag;  
  35.     }  
  36.     #endregion  
  37. }  

 

 

结果如下:Use SqlServer Insert;RecordCount:40000;BatchSize:1;Time:62329;

 

 

可以根据批量大小拼接sql减少服务器的往返次数,我们测试下在批处理10,20,50,100,200下的插入时间

[c-sharp]  view plain copy
  1. #region 拼接sql语句插入  
  2. public static bool ExecuteBatchInsert(DataTable dt, int batchSize)  
  3. {  
  4.     int count = dt.Rows.Count;  
  5.     StringBuilder sql = new StringBuilder(220);  
  6.     bool flag = false;  
  7.     SqlConnection cn = null;  
  8.     SqlCommand cmd = null;  
  9.     try  
  10.     {  
  11.         cn = new SqlConnection(connectionString);  
  12.         cmd = new SqlCommand();  
  13.         cmd.Connection = cn;  
  14.         cn.Open();  
  15.         for (int i = 0; i < count; i += batchSize)  
  16.         {  
  17.             for (int j = i; j < i + batchSize && j < count; j++)  
  18.             {  
  19.                 sql.AppendFormat("Insert into TestTable(Id, Name) Values({0}, '{1}');", dt.Rows[j]["Id"], dt.Rows[j]["Name"]);  
  20.             }  
  21.             //LogHelper.Info(sql.ToString());  
  22.             cmd.CommandText = sql.ToString();  
  23.             cmd.ExecuteNonQuery();  
  24.             sql.Clear();  
  25.         }  
  26.         flag = true;  
  27.     }  
  28.     catch (Exception ex)  
  29.     {  
  30.         LogHelper.Error(sql.ToString(), ex);  
  31.         return false;  
  32.     }  
  33.     finally  
  34.     {  
  35.         if (cn != null)  
  36.         {  
  37.             if (cn.State == ConnectionState.Open)  
  38.             {  
  39.                 cn.Close();  
  40.             }  
  41.             cn.Dispose();  
  42.         }  
  43.         if (cmd != null) cmd.Dispose();  
  44.     }  
  45.     return flag;  
  46. }  
  47. #endregion  

 

结果如下:

Use SqlServer Batch Insert;RecordCount:40000;BatchSize:10;Time:30010;

Use SqlServer Batch Insert;RecordCount:40000;BatchSize:20;Time:21858;

Use SqlServer Batch Insert;RecordCount:40000;BatchSize:50;Time:17564;

Use SqlServer Batch Insert;RecordCount:40000;BatchSize:100;Time:19869;

Use SqlServer Batch Insert;RecordCount:40000;BatchSize:200;Time:17936;

插入时间为前者的28% ~ 48%看来减少服务器的往返次数还是有效果的,批量50条基本是平衡点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值