人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

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

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

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

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

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

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

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

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

這是mysql實(shí)例類(lèi);此實(shí)例提供mysql的連接,和數(shù)據(jù)庫(kù)相關(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;
      }
    }
    //打開(kāi)連接
    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)
    {
      //程序時(shí)間監(jiān)控
      Stopwatch sw = new Stopwatch();
      //程序計(jì)時(shí)開(kāi)始
      sw.Start();
      OpenConnect();
      //事務(wù)開(kāi)始
      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();
      //計(jì)時(shí)停止
      sw.Stop();
      TimeSpan ts2 = sw.Elapsed;
      Console.WriteLine(ts2.TotalMilliseconds);
      return count;
    }
    //查詢(xún)出來(lái)的是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>
    /// 查詢(xún)出來(lái)的是數(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個(gè)插入語(yǔ)句一次性提交
    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);
    }
 }

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

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

貼上測(cè)試程序代碼:

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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)本站的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

海外服務(wù)器租用

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

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線(xiàn)路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線(xiàn)

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線(xiàn)
客服

在線(xiàn)客服:7*24小時(shí)在線(xiàn)

客服
熱線(xiàn)

400-630-3752
7*24小時(shí)客服服務(wù)熱線(xiàn)

關(guān)注
微信

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