国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

mysql數(shù)據(jù)插入效率比較

2024-07-25 19:09:31
字體:
供稿:網(wǎng)友

做數(shù)據(jù)插入時,發(fā)現(xiàn)之前上班做哪些辦公系統(tǒng)壓根就沒考慮過數(shù)據(jù)庫性能這些,因為涉及的數(shù)據(jù)量小,時間和效率看不出來,可當(dāng)數(shù)據(jù)量很大了,大到了每秒需要10000次插入時,這時就不得不考慮你的sql 語句了。當(dāng)插入100條數(shù)據(jù),能想到的數(shù)據(jù)插入方式:

1:for循環(huán)100次,一次次插入數(shù)據(jù)。連接一次插入100次,這樣是最費時間的也是最費IO和連接的;

2:將100數(shù)據(jù)插入語句組成一個sql語句,然后連接一次,插入數(shù)據(jù)。這種費時比第一種要好。

3:使用事物,100次插入,最后一次事物commit; 這種比第二種更快;

4:使用insert語句本身的多數(shù)據(jù)插入;

當(dāng)以上方法在少量的數(shù)據(jù)面前,幾乎沒什么差別,我們壓根感覺不出來。可是,當(dāng)數(shù)據(jù)量稍微提大點,比如一次10000條數(shù)據(jù)。插入的速度效率就出來;

這是mysql實例類;此實例提供mysql的連接,和數(shù)據(jù)庫相關(guān)操作

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();    }    //關(guān)閉連接    public static void CloseConnect()    {      mysqlConnect.Close();    }    public static MySqlConnection Connection    {      get      {        return mysqlConnect;      }    }    //防注入方式的插入數(shù)據(jù)    //使用事務(wù) 10000插入,最后才一次事務(wù)提交    public static int InsertData(string Command, List<MySqlParameter> Params)    {      //程序時間監(jiān)控      Stopwatch sw = new Stopwatch();      //程序計時開始      sw.Start();      OpenConnect();      //事務(wù)開始      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++;      }      //事務(wù)提交      trans.Commit();      CloseConnect();      mysqlCommand.Parameters.Clear();      //計時停止      sw.Stop();      TimeSpan ts2 = sw.Elapsed;      Console.WriteLine(ts2.TotalMilliseconds);      return count;    }    //查詢出來的是MySqlDataReader 要使用就不能關(guān)閉連接    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>    /// 查詢出來的是數(shù)據(jù)集合    /// </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;    }    //安全插入 參數(shù)使用@    //不使用事務(wù) 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);    } }

最后建立控制臺程序,分別使用事務(wù)提交,不使用事務(wù),和拼接10000條插入在組成事務(wù),這三種方式做一個測試,打印出耗時。結(jié)果如圖:

mysql,數(shù)據(jù)插入

可以看到:10000次插入使用事務(wù)提交只用時4.7秒,而不使用事務(wù)用時311秒,拼裝成10000次insert語句的耗時7.3秒。這里面耗時7.3秒的,理論上,在數(shù)據(jù)庫sql執(zhí)行上也應(yīng)該和使用事務(wù)差不多,這里的耗時主要是用作字符串的拼接上,客戶端耗時比較多;

貼上測試程序代碼:

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();    }    //使用安全防注入 參數(shù)使用@ ,安全插入。    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();    }   }}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到MYSQL教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 思南县| 永年县| 洪雅县| 淳化县| 夏河县| 晋中市| 永和县| 台安县| 布尔津县| 方山县| 平江县| 东海县| 堆龙德庆县| 玉龙| 北流市| 襄汾县| 云和县| 四川省| 松潘县| 武山县| 滁州市| 邳州市| 桐乡市| 通辽市| 昌江| 中卫市| 通海县| 周至县| 沛县| 阳西县| 韶关市| 屏南县| 体育| 镇赉县| 徐闻县| 泸水县| 宽城| 海淀区| 克拉玛依市| 商河县| 无为县|