Dapper和原生sql 速度比较

1、表中有三个字段,已经有100多万条数据

  秒 Dapper批量Model插入时间:40.6165513,Dapper单条Model插入时间:95.9492972,Dapper单条sql插入时间:91.0191095,原始单条sql插入时间:90.5096905

  秒 Dapper批量Model插入时间:40.4729053,Dapper单条Model插入时间:99.0270506,Dapper单条sql插入时间:92.7325932,原始单条sql插入时间:91.9713511

  秒 Dapper批量Model插入时间:41.0260065,Dapper单条Model插入时间:95.8173737,Dapper单条sql插入时间:90.9012987,原始单条sql插入时间:90.2153092

  秒 Dapper批量Model插入时间:41.5675273,Dapper单条Model插入时间:101.9446306,Dapper单条sql插入时间:94.4770289,原始单条sql插入时间:92.9758614

 1 #region +Insert 新增Model
 2         /// <summary>
 3         /// 新增 
 4         /// </summary>
 5         /// <returns></returns>
 6         public int Insert<T>(T t) where T : class, new()
 7         {
 8             int result = 0;
 9             try
10             {
11                 using (MySqlConnection con = new MySqlConnection(connection))
12                 {
13                     string strSqlText = GetSqlInsert<T>(t);
14                     result = con.Execute(strSqlText, t);
15                 }
16             }
17             catch (Exception ex)
18             {
19                 WriteLog(ex.ToString());
20             }
21             return result;
22         }
23 
24         private string GetSqlInsert<T>(T t)
25         {
26             Type type = t.GetType();
27             PropertyInfo[] properties = type.GetProperties();
28             string sqlText = "INSERT INTO {0} ({1}) VALUES ({2})";
29 
30             StringBuilder fileds = new StringBuilder();
31             StringBuilder values = new StringBuilder();
32             foreach (var proper in properties)
33             {
34                 if (!proper.CustomAttributes.Any(x => x.AttributeType == typeof(AutoKeyAttribute))
35                     && !proper.CustomAttributes.Any(x => x.AttributeType == typeof(DefaultAttribute)))
36                 {
37                     fileds.Append(proper.Name + ",");
38                     values.Append("@" + proper.Name + ",");
39                 }
40             }
41             sqlText = string.Format(sqlText, type.Name, fileds.ToString().TrimEnd(','), values.ToString().TrimEnd(','));
42             return sqlText;
43         }
44 
45         #endregion
 1 #region +InsertBulk 批量新增
 2         /// <summary>
 3         /// 批量新增 
 4         /// </summary>
 5         /// <returns></returns>
 6         public int InsertBulk<T>(List<T> list) where T : class, new()
 7         {
 8             int result = 0;
 9             try
10             {
11                 using (MySqlConnection con = new MySqlConnection(connection))
12                 {
13                     string strSqlText = GetSqlInsertBulk<T>(list);
14                     result = con.Execute(strSqlText, list);
15                 }
16             }
17             catch (Exception ex)
18             {
19                 WriteLog(ex.ToString());
20             }
21             return result;
22         }
23 
24 
25         private string GetSqlInsertBulk<T>(List<T> list)
26         {
27             return list.Count() > 0 ? GetSqlInsert(list[0]) : "";
28         }
29 
30         #endregion
 1 public int ExecuteNonQuery1(string sql)
 2         {
 3             using (MySqlConnection conn = new MySqlConnection(connection))
 4             {
 5                 MySqlCommand cmd = new MySqlCommand();
 6                 if (conn.State != ConnectionState.Open)
 7                     conn.Open();
 8 
 9                 cmd.Connection = conn;
10                 cmd.CommandText = sql;
11                 cmd.CommandType = CommandType.Text;
12                 return cmd.ExecuteNonQuery();
13             }
14         }
View Code

猜你喜欢

转载自www.cnblogs.com/liangwenchao-912/p/9106040.html
今日推荐