五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

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

發(fā)布日期:2022-03-15 19:07 | 文章來源:gibhub

做數(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é)果如圖:

可以看到: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í)價值,謝謝大家對本站的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部