一、表中有三個字段,已經有100多萬條數據,每次插入10萬條數據 時間單位:秒sql
秒 Dapper批量Model插入時間:40.6165513,Dapper單條Model插入時間:95.9492972,Dapper單條sql插入時間:91.0191095,原生單條sql插入時間:90.5096905app
秒 Dapper批量Model插入時間:40.4729053,Dapper單條Model插入時間:99.0270506,Dapper單條sql插入時間:92.7325932,原生單條sql插入時間:91.9713511ide
秒 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.9758614ui
測試時間代碼spa
List<gk_area> lst = new List<gk_area>(); for (int i = 0; i < 100000; i++) { gk_area tmp = new gk_area(); tmp.gk_AreaId = Guid.NewGuid().ToString("N"); tmp.gk_AreaName = "測試:" + tmp.gk_AreaId; tmp.gk_DelFlag = "0"; lst.Add(tmp); } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 new DapperHelper().InsertBulk<gk_area>(lst); // 須要測試的代碼 .... stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "秒 Dapper批量Model插入時間:" + seconds + ","; } lst = new List<gk_area>(); for (int i = 0; i < 100000; i++) { gk_area tmp = new gk_area(); tmp.gk_AreaId = Guid.NewGuid().ToString("N"); tmp.gk_AreaName = "測試:" + tmp.gk_AreaId; tmp.gk_DelFlag = "0"; lst.Add(tmp); } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... foreach (var item in lst) { new DapperHelper().Insert<gk_area>(item); } stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "Dapper單條Model插入時間:" + seconds + ","; } lst = new List<gk_area>(); for (int i = 0; i < 100000; i++) { gk_area tmp = new gk_area(); tmp.gk_AreaId = Guid.NewGuid().ToString("N"); tmp.gk_AreaName = "測試:" + tmp.gk_AreaId; tmp.gk_DelFlag = "0"; lst.Add(tmp); } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... foreach (var item in lst) { new DapperHelper().ExcuteNonQuery("insert into gk_area (gk_AreaId,gk_AreaName) values ('" + item.gk_AreaId + "','" + item.gk_AreaName + "')"); } stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "Dapper單條sql插入時間:" + seconds + ","; } lst = new List<gk_area>(); for (int i = 0; i < 100000; i++) { gk_area tmp = new gk_area(); tmp.gk_AreaId = Guid.NewGuid().ToString("N"); tmp.gk_AreaName = "測試:" + tmp.gk_AreaId; tmp.gk_DelFlag = "0"; lst.Add(tmp); } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... foreach (var item in lst) { new DapperHelper().ExecuteNonQuery1("insert into gk_area (gk_AreaId,gk_AreaName) values ('" + item.gk_AreaId + "','" + item.gk_AreaName + "')"); } stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "原始單條sql插入時間:" + seconds + ","; }
插庫方法pwa
#region +Insert 新增Model /// <summary> /// 新增 /// </summary> /// <returns></returns> public int Insert<T>(T t) where T : class, new() { int result = 0; try { using (MySqlConnection con = new MySqlConnection(connection)) { string strSqlText = GetSqlInsert<T>(t); result = con.Execute(strSqlText, t); } } catch (Exception ex) { WriteLog(ex.ToString()); } return result; } private string GetSqlInsert<T>(T t) { Type type = t.GetType(); PropertyInfo[] properties = type.GetProperties(); string sqlText = "INSERT INTO {0} ({1}) VALUES ({2})"; StringBuilder fileds = new StringBuilder(); StringBuilder values = new StringBuilder(); foreach (var proper in properties) { if (!proper.CustomAttributes.Any(x => x.AttributeType == typeof(AutoKeyAttribute)) && !proper.CustomAttributes.Any(x => x.AttributeType == typeof(DefaultAttribute))) { fileds.Append(proper.Name + ","); values.Append("@" + proper.Name + ","); } } sqlText = string.Format(sqlText, type.Name, fileds.ToString().TrimEnd(','), values.ToString().TrimEnd(',')); return sqlText; } #endregion #region +InsertBulk 批量新增 /// <summary> /// 批量新增 /// </summary> /// <returns></returns> public int InsertBulk<T>(List<T> list) where T : class, new() { int result = 0; try { using (MySqlConnection con = new MySqlConnection(connection)) { string strSqlText = GetSqlInsertBulk<T>(list); result = con.Execute(strSqlText, list); } } catch (Exception ex) { WriteLog(ex.ToString()); } return result; } private string GetSqlInsertBulk<T>(List<T> list) { return list.Count() > 0 ? GetSqlInsert(list[0]) : ""; } #endregion /// <summary> /// 原始sql /// </summary> /// <param name="sql"></param> /// <returns></returns> public int ExecuteNonQuery1(string sql) { using (MySqlConnection conn = new MySqlConnection(connection)) { MySqlCommand cmd = new MySqlCommand(); if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; return cmd.ExecuteNonQuery(); } }
二、表中存在3029949(300萬)條數據 時間單位:秒3d
反射字段查詢時間:22.651388,Dapper查詢時間:9.7697742,原生查詢1時間:13.8903755,原生查詢2時間:16.1955305code
反射字段查詢時間:21.3172684,Dapper查詢時間:9.7593554,原生查詢1時間:14.3009033,原生查詢2時間:15.085577orm
反射字段查詢時間:21.8932407,Dapper查詢時間:8.3832515,原生查詢1時間:13.4945138,原生查詢2時間:17.7821992
反射字段查詢時間:21.9676583,Dapper查詢時間:9.3833032,原生查詢1時間:13.5412978,原生查詢2時間:15.5048999
測試時間代碼
if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... var test1 = new DapperHelper().FindToList<gk_area>("select * from gk_area"); stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "反射字段查詢時間:" + seconds + ","; } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... var test2 = new DapperHelper().QueryToList<gk_area>("select * from gk_area"); stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "Dapper查詢時間:" + seconds + ","; } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... var test3 = new DapperHelper().GetDataTable("select * from gk_area"); stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "原始查詢時間:" + seconds + ","; } if (true) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 開始監視代碼運行時間 // 須要測試的代碼 .... var test3 = new DapperHelper().ExecuteDataTable("select * from gk_area"); stopwatch.Stop(); // 中止監視 TimeSpan timespan = stopwatch.Elapsed; // 獲取當前實例測量得出的總時間 double seconds = timespan.TotalSeconds; // 總秒數 timeStr += "原始查詢2時間:" + seconds + ","; }
查庫方法
#region +FindToList 查詢數據集合 反射字段查詢 廢棄 /// <summary> /// 同步查詢數據集合 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="sql">sql語句</param> /// <param name="param">參數</param> /// <param name="flag">true存儲過程,false sql語句</param> /// <returns>t</returns> public List<T> FindToList<T>(string sql, DynamicParameters param = null, bool flag = false) where T : class, new() { IDataReader dataReader = null; using (MySqlConnection con = new MySqlConnection(connection)) { if (flag) { dataReader = con.ExecuteReader(sql, param, null, null, CommandType.StoredProcedure); } else { dataReader = con.ExecuteReader(sql, param, null, null, CommandType.Text); } if (dataReader == null || !dataReader.Read()) return null; Type type = typeof(T); List<T> tlist = new List<T>(); do { T t = new T(); foreach (var item in type.GetProperties()) { for (int i = 0; i < dataReader.FieldCount; i++) { //屬性名與查詢出來的列名比較 if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue; var kvalue = dataReader[item.Name]; if (kvalue == DBNull.Value) continue; item.SetValue(t, kvalue, null); break; } } if (tlist != null) tlist.Add(t); } while (dataReader.Read()); return tlist; } } #endregion #region +QueryToList Dapper 查詢數據集合 /// <summary> /// 同步查詢數據集合 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="sql">sql語句</param> /// <param name="param">參數</param> /// <param name="flag">true存儲過程,false sql語句</param> /// <returns>t</returns> public List<T> QueryToList<T>(string sql, DynamicParameters param = null, bool flag = false) where T : class, new() { List<T> list = null; try { using (MySqlConnection con = new MySqlConnection(connection)) { if (flag) { list = con.Query<T>(sql, param, null, true, null, CommandType.StoredProcedure) as List<T>; } else { list = con.Query<T>(sql, param, null, true, null, CommandType.Text) as List<T>; } } } catch (Exception ex) { WriteLog(ex.ToString()); } return list; } #endregion /// <summary> /// 原始查詢1 /// </summary> /// <param name="sql"></param> /// <returns></returns> public DataTable GetDataTable(string sql) { //建立一個MySqlCommand對象 MySqlCommand cmd = new MySqlCommand(); //建立一個MySqlConnection對象 MySqlConnection conn = new MySqlConnection(connection); try { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = cmd; DataTable ds = new DataTable(); adapter.Fill(ds); //清除參數 cmd.Parameters.Clear(); conn.Close(); return ds; } catch (Exception e) { throw e; } } /// <summary> /// 原始查詢2 /// </summary> /// <param name="SQLString"></param> /// <returns></returns> public DataTable ExecuteDataTable(string SQLString) { using (MySqlConnection conn = new MySqlConnection(connection)) { DataSet ds = new DataSet(); try { conn.Open(); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, conn); command.Fill(ds, "ds"); } catch (MySql.Data.MySqlClient.MySqlException ex) { throw new Exception(ex.Message); } return ds.Tables[0]; } }