麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

.NET批量大數據插入性能分析及比較

2019-11-14 16:03:58
字體:
來源:轉載
供稿:網友

 

數據插入使用了以下幾種方式

1. 逐條數據插入
2. 拼接sql語句批量插入
3. 拼接sql語句并使用Transaction
4. 拼接sql語句并使用SqlTransaction
5. 使用DataAdapter
6. 使用TransactionScope及SqlBulkCopy
7. 使用表值參數

 

數據庫使用SQL Server,腳本如下

 

create table TestTable
(
Id int
,Name nvarchar(20)
)

 

程序中生成測試DataTable結構和測試數據的類如下

[c-sharp] view plaincopyPRint?
1.public class Tools 
2.{ 
3.    public static DataTable MakeDataTable() 
4.    { 
5.        DataTable table = new DataTable(); 
6. 
7.        //生成DataTable的模式(schema)  
8.        table.Columns.Add("Id", Type.GetType("System.Int32")); 
9.        table.Columns.Add("Name", Type.GetType("System.String")); 
10. 
11.        //設置主鍵  
12.        table.PrimaryKey = new DataColumn[] { table.Columns["ID"] }; 
13.        table.Columns["Id"].AutoIncrement = true; 
14.        table.Columns["Id"].AutoIncrementSeed = 1; 
15.        table.Columns["Id"].ReadOnly = true; 
16.        return table; 
17.    } 
18. 
19.    public static void MakeData(DataTable table, int count) 
20.    { 
21.        if (table == null) 
22.            return; 
23. 
24.        if (count <= 0) 
25.            return; 
26. 
27.        DataRow row = null; 
28. 
29.        for (int i = 1; i <= count; i++) 
30.        { 
31.            //創建一個新的DataRow對象(生成一個新行)  
32.            row = table.NewRow(); 
33.            row["Name"] = "Test" + i.ToString(); 
34.            //添加新的DataRow  
35.            table.Rows.Add(row); 
36.        } 
37.    } 
38.} 
    public class Tools
    {
        public static DataTable MakeDataTable()
        {
            DataTable table = new DataTable();

            //生成DataTable的模式(schema)
            table.Columns.Add("Id", Type.GetType("System.Int32"));
            table.Columns.Add("Name", Type.GetType("System.String"));

            //設置主鍵
            table.PrimaryKey = new DataColumn[] { table.Columns["ID"] };
            table.Columns["Id"].AutoIncrement = true;
            table.Columns["Id"].AutoIncrementSeed = 1;
            table.Columns["Id"].ReadOnly = true;
            return table;
        }

        public static void MakeData(DataTable table, int count)
        {
            if (table == null)
                return;

            if (count <= 0)
                return;

            DataRow row = null;

            for (int i = 1; i <= count; i++)
            {
                //創建一個新的DataRow對象(生成一個新行)
                row = table.NewRow();
                row["Name"] = "Test" + i.ToString();
                //添加新的DataRow
                table.Rows.Add(row);
            }
        }
    }

 

 

 

使用Log4net記錄日志,默認插入記錄數為40000條,每次插入1條,可在界面修改,使用System.Diagnostics.StopWatch記錄插入時間,每次測試后刪除原表重建

 

窗體代碼如下:

 

[c-sharp] www.nuoya118.com
  1. public delegate bool InsertHandler(DataTable table, int batchSize);  
  2.   
  3. public partial class FrmBatch : Form  
  4. {  
  5.     private Stopwatch _watch = new Stopwatch();  
  6.   
  7.     public FrmBatch()  
  8.     {  
  9.         InitializeComponent();  
  10.     }  
  11.   
  12.     private void FrmBatch_Load(object sender, EventArgs e)  
  13.     {  
  14.         txtRecordCount.Text = "40000";  
  15.         txtBatchSize.Text = "1";  
  16.     }  
  17.   
  18.     //逐條數據插入   
  19.     private void btnInsert_Click(object sender, EventArgs e)  
  20.     {  
  21.         Insert(DbOperation.ExecuteInsert, "Use SqlServer Insert");  
  22.     }  
  23.   
  24.     //拼接sql語句插入   
  25.     private void btnBatchInsert_Click(object sender, EventArgs e)  
  26.     {  
  27.         Insert(DbOperation.ExecuteBatchInsert, "Use SqlServer Batch Insert");  
  28.     }  
  29.   
  30.     //拼接sql語句并使用Transaction   
  31.     private void btnTransactionInsert_Click(object sender, EventArgs e)  
  32.     {  
  33.         Insert(DbOperation.ExecuteTransactionInsert, "Use SqlServer Batch Transaction Insert");  
  34.     }  
  35.   
  36.     //拼接sql語句并使用SqlTransaction   
  37.     private void btnSqlTransactionInsert_Click(object sender, EventArgs e)  
  38.     {  
  39.         Insert(DbOperation.ExecuteSqlTransactionInsert, "Use SqlServer Batch SqlTransaction Insert");  
  40.     }  
  41.   
  42.     //使用DataAdapter   
  43.     private void btnDataAdapterInsert_Click(object sender, EventArgs e)  
  44.     {  
  45.         Insert(DbOperation.ExecuteDataAdapterInsert, "Use SqlServer DataAdapter Insert");  
  46.     }  
  47.   
  48.     //使用TransactionScope   
  49.     private void btnTransactionScopeInsert_Click(object sender, EventArgs e)  
  50.     {  
  51.         Insert(DbOperation.ExecuteTransactionScopeInsert, "Use SqlServer TransactionScope Insert");  
  52.     }  
  53.   
  54.     //使用表值參數   
  55.     private void btnTableTypeInsert_Click(object sender, EventArgs e)  
  56.     {  
  57.         Insert(DbOperation.ExecuteTableTypeInsert, "Use SqlServer TableType Insert");  
  58.     }  
  59.   
  60.     private DataTable InitDataTable()  
  61.     {  
  62.         DataTable table = Tools.MakeDataTable();  
  63.         int count = 0;  
  64.         if (int.TryParse(txtRecordCount.Text.Trim(), out count))  
  65.         {  
  66.             Tools.MakeData(table, count);  
  67.             //MessageBox.Show("Data Init OK");   
  68.         }  
  69.         return table;  
  70.     }  
  71.   
  72.     public void Insert(InsertHandler handler, string msg)  
  73.     {  
  74.         DataTable table = InitDataTable();  
  75.         if (table == null)  
  76.         {  
  77.             MessageBox.Show("DataTable is null");  
  78.             return;  
  79.         }  
  80.   
  81.         int recordCount = table.Rows.Count;  
  82.         if (recordCount <= 0)  
  83.         {  
  84.             MessageBox.Show("No Data");  
  85.             return;  
  86.         }  
  87.   
  88.         int batchSize = 0;  
  89.         int.TryParse(txtBatchSize.Text.Trim(), out batchSize);  
  90.         if (batchSize <= 0)  
  91.         {  
  92.             MessageBox.Show("batchSize <= 0");  
  93.             return;  
  94.         }  
  95.   
  96.         bool result = false;  
  97.         _watch.Reset(); _watch.Start();  
  98.         result = handler(table, batchSize);  
  99.         _watch.Stop(www.nuoya66.com);  
  100.         string log = string.Format("{0};RecordCount:{1};BatchSize:{2};Time:{3};", msg, recordCount, batchSize, _watch.ElapsedMilliseconds);  
  101.         LogHelper.Info(log);  
  102.         MessageBox.Show(result.ToString());  
  103.     }  
  104. }  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 神马久久蜜桃 | 国产精品成人免费一区久久羞羞 | h视频免费看 | 性大片免费看 | 欧美交在线 | 暖暖免费观看高清完整版电影 | 久久另类视频 | 欧美成人一区二区三区 | 日本中文字幕高清 | 国产一级中文字幕 | 欧美日韩亚洲国产精品 | 亚洲天堂成人在线观看 | 欧美18—19sex性护士中国 | 成人在线视频一区 | 日韩黄站 | 亚州综合网| 国产乱一区二区三区视频 | 久久精品视频在线免费观看 | 久久久国产一级片 | 久久久久亚洲精品国产 | 人禽l交免费视频观看 视频 | 国产精品99久久久久久久 | 99国产精品自拍 | 国产精品视频久久久 | 操穴视频| 久久久久久亚洲综合影院红桃 | 99久久精品免费 | 日韩精品中文字幕在线播放 | 欧美一级黄带 | 污污短视频 | 91社区电影| h视频免费观看 | 最新亚洲国产 | 久久精品男人 | 日韩毛片网 | 一级做人爱c黑人影片 | 激情视频在线播放 | 成人电影毛片 | 999精品国产| 亚洲成人在线免费 | 国产精品久久久久久久久久久久午夜 |