欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

mysql數(shù)據(jù)插入效率的示例分析

這篇文章給大家分享的是有關(guān)MySQL數(shù)據(jù)插入效率的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!成都創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務(wù)好的網(wǎng)站設(shè)計公司,做網(wǎng)站、成都做網(wǎng)站負(fù)責(zé)任的成都網(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();
    }
   }
}

感謝各位的閱讀!關(guān)于“mysql數(shù)據(jù)插入效率的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

本文題目:mysql數(shù)據(jù)插入效率的示例分析
URL分享:http://chinadenli.net/article18/joiddp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、外貿(mào)網(wǎng)站建設(shè)、虛擬主機、面包屑導(dǎo)航、域名注冊、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司
在线免费观看一二区视频| 久久精品国产亚洲av久按摩| 夜夜躁狠狠躁日日躁视频黑人| 久久99青青精品免费| 丝袜破了有美女肉体免费观看| 欧美日韩精品视频在线| 九九热精品视频在线观看| 一区二区三区亚洲国产| 日韩精品在线观看一区| 国产熟女一区二区精品视频| 夜夜躁狠狠躁日日躁视频黑人| 亚洲精品深夜福利视频| 福利新区一区二区人口| 激情少妇一区二区三区| 国产传媒一区二区三区| 91欧美日韩精品在线| 亚洲专区中文字幕在线| 日本特黄特色大片免费观看| 亚洲一区二区三区中文久久| 欧美日韩精品一区免费| 69精品一区二区蜜桃视频| 久热香蕉精品视频在线播放| 亚洲二区欧美一区二区| 国产老熟女超碰一区二区三区 | 国产欧美一区二区另类精品| 日韩夫妻午夜性生活视频| 亚洲国产日韩欧美三级| 91亚洲精品国产一区| 久久精品国产一区久久久| 精品一区二区三区乱码中文| 国产精品一区二区视频成人| 国产日韩久久精品一区| 中文字幕一区二区久久综合| 亚洲中文字幕乱码亚洲| 国内女人精品一区二区三区| 好吊日在线视频免费观看| 人妻亚洲一区二区三区| 麻豆精品视频一二三区| 日本91在线观看视频| 亚洲二区欧美一区二区| 国产成人精品午夜福利av免费|