. NET batch big data insertion performance analysis and comparison,. net data insertion Performance
Data insertion uses the following methods:
1. Insert data one by one
2. concatenate SQL statements for batch insert
3. concatenate SQL statements and use Transaction
4. concatenate SQL statements and use SqlTransaction
5. Use DataAdapter
6. Use TransactionScope and SqlBulkCopy
7. Use table value Parameters
The database uses SQL Server. The script is as follows:
Create table TestTable
(
Id int
, Name nvarchar (20)
)
The program generates the test DataTable structure and test data class as follows:
[C-sharp] view plaincopyprint?
1. public class Tools
2 .{
3. public static DataTable MakeDataTable ()
4 .{
5. DataTable table = new DataTable ();
6.
7. // schema for generating DataTable)
8. table. Columns. Add ("Id", Type. GetType ("System. Int32 "));
9. table. Columns. Add ("Name", Type. GetType ("System. String "));
10.
11. // set the primary key
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. // create a new DataRow object (generate a new row)
32. row = table. NewRow ();
33. row ["Name"] = "Test" + I. ToString ();
34. // Add a new DataRow
35. table. Rows. Add (row );
36 .}
37 .}
38 .}
Public class Tools
{
Public static DataTable MakeDataTable ()
{
DataTable table = new DataTable ();
// Schema)
Table. Columns. Add ("Id", Type. GetType ("System. Int32 "));
Table. Columns. Add ("Name", Type. GetType ("System. String "));
// Set the 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 );
}
}
}
Use Log4net to record logs. The default number of inserted records is 40000. you can insert one record at a time. You can modify it on the interface and use System. Diagnostics. StopWatch to record the insertion time. After each test, delete the original table and recreate it.
The form code is as follows:
[C-sharp]Www.nuoya118.com
- Public delegate bool InsertHandler (DataTable table, int batchSize );
- Public partial class FrmBatch: Form
- {
- Private Stopwatch _ watch = new Stopwatch ();
- Public FrmBatch ()
- {
- InitializeComponent ();
- }
- Private void FrmBatch_Load (object sender, EventArgs e)
- {
- TxtRecordCount. Text = "40000 ";
- TxtBatchSize. Text = "1 ";
- }
- // Insert data one by one
- Private void btnInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteInsert, "Use SqlServer Insert ");
- }
- // Concatenate SQL statement insert
- Private void btnBatchInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteBatchInsert, "Use SqlServer Batch Insert ");
- }
- // Concatenate an SQL statement and use Transaction
- Private void btnTransactionInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteTransactionInsert, "Use SqlServer Batch Transaction Insert ");
- }
- // Concatenate an SQL statement and use SqlTransaction
- Private void btnSqlTransactionInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteSqlTransactionInsert, "Use SqlServer Batch SqlTransaction Insert ");
- }
- // Use DataAdapter
- Private void btnDataAdapterInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteDataAdapterInsert, "Use SqlServer DataAdapter Insert ");
- }
- // Use TransactionScope
- Private void btnTransactionScopeInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteTransactionScopeInsert, "Use SqlServer TransactionScope Insert ");
- }
- // Use table value Parameters
- Private void btnTableTypeInsert_Click (object sender, EventArgs e)
- {
- Insert (DbOperation. ExecuteTableTypeInsert, "Use SqlServer TableType Insert ");
- }
- Private DataTable InitDataTable ()
- {
- DataTable table = Tools. MakeDataTable ();
- Int count = 0;
- If (int. TryParse (txtRecordCount. Text. Trim (), out count ))
- {
- Tools. MakeData (table, count );
- // MessageBox. Show ("Data Init OK ");
- }
- Return table;
- }
- Public void Insert (InsertHandler handler, string msg)
- {
- DataTable table = InitDataTable ();
- If (table = null)
- {
- MessageBox. Show ("DataTable is null ");
- Return;
- }
- Int recordCount = table. Rows. Count;
- If (recordCount <= 0)
- {
- MessageBox. Show ("No Data ");
- Return;
- }
- Int batchSize = 0;
- Int. TryParse (txtBatchSize. Text. Trim (), out batchSize );
- If (batchSize <= 0)
- {
- MessageBox. Show ("batchSize <= 0 ");
- Return;
- }
- Bool result = false;
- _ Watch. Reset (); _ watch. Start ();
- Result = handler (table, batchSize );
- _ Watch. Stop (www.nuoya66.com );
- String log = string. format ("{0}; RecordCount: {1}; BatchSize: {2}; Time: {3};", msg, recordCount, batchSize, _ watch. elapsedMilliseconds );
- LogHelper. Info (log );
- MessageBox. Show (result. ToString ());
- }
- }