mysql数据插入效率比较
做数据插入时,发现之前上班做哪些办公系统压根就没考虑过数据库性能这些,因为涉及的数据量小,时间和效率看不出来,可当数据量很大了,大到了每秒需要10000次插入时,这时就不得不考虑你的sql 语句了。当插入100条数据,能想到的数据插入方式:
1:for循环100次,一次次插入数据。连接一次插入100次,这样是最费时间的也是最费IO和连接的;
2:将100数据插入语句组成一个sql语句,然后连接一次,插入数据。这种费时比第一种要好。
3:使用事物,100次插入,最后一次事物commit; 这种比第二种更快;
4:使用insert语句本身的多数据插入;
当以上方法在少量的数据面前,几乎没什么差别,我们压根感觉不出来。可是,当数据量稍微提大点,比如一次10000条数据。插入的速度效率就出来;
这是mysql实例类;此实例提供mysql的连接,和数据库相关操作
public class MySqlInstance { //连接字符串 private static string mySqlConnectionStr = "Server =localhost;Database=test;Uid=root;Pwd=password.1;"; private static MySqlConnection _mysqlConnect; private static MySqlConnection mysqlConnect { get { if (null == _mysqlConnect) { _mysqlConnect = new MySqlConnection(mySqlConnectionStr); } return _mysqlConnect; } } private static MySqlCommand _mysqlCommand; private static MySqlCommand mysqlCommand { get { if (null == _mysqlCommand) { _mysqlCommand = mysqlConnect.CreateCommand(); } return _mysqlCommand; } } //打开连接 public static void OpenConnect() { mysqlConnect.Open(); } //关闭连接 public static void CloseConnect() { mysqlConnect.Close(); } public static MySqlConnection Connection { get { return mysqlConnect; } } //防注入方式的插入数据 //使用事务 10000插入,最后才一次事务提交 public static int InsertData(string Command, List<MySqlParameter> Params) { //程序时间监控 Stopwatch sw = new Stopwatch(); //程序计时开始 sw.Start(); OpenConnect(); //事务开始 MySqlTransaction trans = mysqlConnect.BeginTransaction(); mysqlCommand.CommandText = Command; mysqlCommand.Parameters.AddRange(Params.ToArray()); int count = 0; for (int i = 0; i < 10000; i++) { if (mysqlCommand.ExecuteNonQuery() > 0) count++; } //事务提交 trans.Commit(); CloseConnect(); mysqlCommand.Parameters.Clear(); //计时停止 sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine(ts2.TotalMilliseconds); return count; } //查询出来的是MySqlDataReader 要使用就不能关闭连接 public static MySqlDataReader SelectData(string sql) { Stopwatch sw = new Stopwatch(); sw.Start(); // OpenConnect(); MySqlCommand newcommond = new MySqlCommand(sql, mysqlConnect); MySqlDataReader data = newcommond.ExecuteReader(); // CloseConnect(); sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine(ts2.TotalMilliseconds); return data; } /// <summary> /// 查询出来的是数据集合 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataSet SelectDataSet(string sql) { MySqlCommand newcommond = new MySqlCommand(sql, mysqlConnect); MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = newcommond; DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } //不安全插入 有注入 public static int InsertDataSql(string sql) { // OpenConnect(); mysqlCommand.CommandText = sql; int count = mysqlCommand.ExecuteNonQuery(); // CloseConnect(); return count; } //安全插入 参数使用@ //不使用事务 10000次插入 public static int InsertDataNoTran(string Command, List<MySqlParameter> Params) { Stopwatch sw = new Stopwatch(); sw.Start(); OpenConnect(); mysqlCommand.CommandText = Command; mysqlCommand.Parameters.AddRange(Params.ToArray()); int count = 0; for (int i = 0; i < 10000; i++) { if (mysqlCommand.ExecuteNonQuery() > 0) count++; } CloseConnect(); mysqlCommand.Parameters.Clear(); sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine(ts2.TotalMilliseconds); return count; } //一次性拼10000个插入语句一次性提交 public static void test4() { Stopwatch sw = new Stopwatch(); sw.Start(); MySqlInstance.OpenConnect(); MySqlTransaction tran = MySqlInstance.Connection.BeginTransaction(); string command = string.Empty; for (int i = 0; i < 10000; i++) { string temp = string.Format("insert into test.testtable(pname,pwd) value ('{0}','{1}'); \r\n", "name" + i, "password." + i); command += temp; } MySqlInstance.InsertDataSql(command); tran.Commit(); MySqlInstance.CloseConnect(); sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine(ts2.TotalMilliseconds); } }
最后建立控制台程序,分别使用事务提交,不使用事务,和拼接10000条插入在组成事务,这三种方式做一个测试,打印出耗时。结果如图:
可以看到:10000次插入使用事务提交只用时4.7秒,而不使用事务用时311秒,拼装成10000次insert语句的耗时7.3秒。这里面耗时7.3秒的,理论上,在数据库sql执行上也应该和使用事务差不多,这里的耗时主要是用作字符串的拼接上,客户端耗时比较多;
贴上测试程序代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data; using MySql.Web; using MySql.Data.MySqlClient; using System.Diagnostics; using System.Data; namespace mysqlDEMO01 { class Program { static void Main(string[] args) { testInsert(); Console.ReadLine(); } //使用安全防注入 参数使用@ ,安全插入。 public static void testInsert() { List<MySqlParameter> lmp = new List<MySqlParameter>(); lmp.Add(new MySqlParameter("@pname", "hello2")); lmp.Add(new MySqlParameter("@pwd", "1232")); string command = " insert into test.testtable(pname,pwd) value(@pname,@pwd); "; MySqlInstance.InsertData(command, lmp); List<MySqlParameter> lmp2 = new List<MySqlParameter>(); lmp2.Add(new MySqlParameter("@pname", "hello2")); lmp2.Add(new MySqlParameter("@pwd", "1232")); MySqlInstance.InsertDataNoTran(command, lmp2); test4(); } } }
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30