. NET batch Big data insert performance analysis and comparison

Source: Internet
Author: User
Tags bulk insert

Data insertion is used in the following ways

1. Insert data by article
2. Splicing SQL statements BULK INSERT
3. Splicing SQL statements and using transaction
4. Splicing SQL statements and using SqlTransaction
5. Using DataAdapter
6. Using TransactionScope and SqlBulkCopy
7. Using table-Valued parameters

The database uses SQL Server and the script is as follows

CREATE TABLE TestTable
(
Id int
, Name nvarchar (20)
)

The class that generates the test DataTable structure and test data in the program is as follows

[C-sharp] View plaincopyprint?
1.public class Tools
0.9
3. public static DataTable makedatatable ()
4. {
5. DataTable table = new DataTable ();
6.
7.//Schema for generating DataTable (schema)
8. Table. Columns.Add ("Id", Type.GetType ("System.Int32"));
9. Table. Columns.Add ("Name", Type.GetType ("System.String"));
10.
11.//Set Primary key
Table. PrimaryKey = new datacolumn[] {table. columns["ID"};
Table. columns["Id"]. AutoIncrement = true;
. table. columns["Id"]. AutoIncrementSeed = 1;
Table. columns["Id"]. ReadOnly = true;
A. return table;
17.}
18.
public static void Makedata (DataTable table, int count)
20. {
. if (table = = null)
A. return;
23.
if (Count <= 0)
return;
26.
. DataRow row = null;
28.
. for (int i = 1; I <= count; i++)
30. {
31.//Create a new DataRow object (to generate a new row)
row = table. NewRow ();
row["Name"] = "Test" + i.tostring ();
34.//Add a new DataRow
Table. Rows.Add (row);
36.}
37.}
38.}
public class Tools
{
public static DataTable makedatatable ()
{
DataTable table = new DataTable ();

           //Schema for generating DataTable (schema)
             table. Columns.Add ("Id", Type.GetType ("System.Int32"));
            table. Columns.Add ("Name", Type.GetType ("System.String"));

           //Set Primary key
             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++)
{
Create a new DataRow object (generate a new row)
row = table. NewRow ();
row["Name"] = "Test" + i.tostring ();
Add a new DataRow
Table. Rows.Add (row);
}
}
}

Using log4net logging, the default number of inserted records is 40,000, each time inserting 1, can be modified in the interface, using System.Diagnostics.StopWatch record insertion time, after each test delete the original table reconstruction

The form code is as follows:

[C-sharp]Www.nuoya118.com
  1. Public Delegate bool Inserthandler (DataTable table, int batchsize);
  2. Public partial class Frmbatch:form
  3. {
  4. private Stopwatch _watch = new Stopwatch ();
  5. Public Frmbatch ()
  6. {
  7. InitializeComponent ();
  8. }
  9. private void Frmbatch_load (object sender, EventArgs e)
  10. {
  11. Txtrecordcount.text = "40000";
  12. Txtbatchsize.text = "1";
  13. }
  14. //Data insertion
  15. private void Btninsert_click (object sender, EventArgs e)
  16. {
  17. Insert (Dboperation.executeinsert, "Use SQL Server Insert");
  18. }
  19. //Splicing SQL statement Insert
  20. private void Btnbatchinsert_click (object sender, EventArgs e)
  21. {
  22. Insert (Dboperation.executebatchinsert, "Use SQL Server Batch Insert");
  23. }
  24. //Splicing SQL statements and using transaction
  25. private void Btntransactioninsert_click (object sender, EventArgs e)
  26. {
  27. Insert (Dboperation.executetransactioninsert, "Use SQL Server Batch Transaction Insert");
  28. }
  29. //Splicing SQL statements and using SqlTransaction
  30. private void Btnsqltransactioninsert_click (object sender, EventArgs e)
  31. {
  32. Insert (Dboperation.executesqltransactioninsert, "Use SQL Server Batch sqltransaction Insert");
  33. }
  34. //Use DataAdapter
  35. private void Btndataadapterinsert_click (object sender, EventArgs e)
  36. {
  37. Insert (Dboperation.executedataadapterinsert, "Use SQL Server DataAdapter Insert");
  38. }
  39. //Use TransactionScope
  40. private void Btntransactionscopeinsert_click (object sender, EventArgs e)
  41. {
  42. Insert (Dboperation.executetransactionscopeinsert, "Use SQL Server TransactionScope Insert");
  43. }
  44. //Using table-valued parameters
  45. private void Btntabletypeinsert_click (object sender, EventArgs e)
  46. {
  47. Insert (Dboperation.executetabletypeinsert, "Use SQL Server Tabletype Insert");
  48. }
  49. private DataTable initdatatable ()
  50. {
  51. DataTable table = tools.makedatatable ();
  52. int count = 0;
  53. if (Int. TryParse (TxtRecordCount.Text.Trim (), out count))
  54. {
  55. Tools.makedata (table, count);
  56. //messagebox.show ("Data Init OK");
  57. }
  58. return table;
  59. }
  60. public void Insert (Inserthandler handler, string msg)
  61. {
  62. DataTable table = initdatatable ();
  63. if (table = = null)
  64. {
  65. MessageBox.Show ("DataTable is null");
  66. return;
  67. }
  68. int recordCount = table.  Rows.Count;
  69. if (recordCount <= 0)
  70. {
  71. MessageBox.Show ("No Data");
  72. return;
  73. }
  74. int batchsize = 0;
  75. Int.  TryParse (TxtBatchSize.Text.Trim (), out batchsize);
  76. if (batchsize <= 0)
  77. {
  78. MessageBox.Show ("batchsize <= 0");
  79. return;
  80. }
  81. bool result = false;
  82. _watch. Reset (); _watch. Start ();
  83. result = handler (table, batchsize);
  84. _watch. Stop (www.nuoya66.com);
  85. string log = string. Format ("{0}; Recordcount:{1}; BATCHSIZE:{2}; TIME:{3}; ", MSG, RecordCount, BatchSize, _watch.  Elapsedmilliseconds);
  86. Loghelper.info (log);
  87. MessageBox.Show (result. ToString ());
  88. }
  89. }

. NET batch Big data insert performance analysis and comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.