SQL Bulk Insert to quickly insert

SQL INSERT INTO statement

INSERT INTO statement is used to insert new records into the table.

SQL INSERT INTO Syntax

INSERT INTO statement can have two forms of writing.

① The first form without specifying the column name you want to insert the data, just to provide value to be inserted:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

② The second form need to specify the column names and values ​​to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

BULK INSERT statement

BULK INSERT for mass data insertion

BULK INSERT Syntax

] FIRSTROW = first_row] - Specifies the number of the first line to be loaded. The default is the first row of the specified data file [[,] FIRE_TRIGGERS] - whether the start trigger












[[,] The FORMATFILE = 'format_file_path']
[[,] the KEEPIDENTITY] - import the identification value of the specified data file for identifying column
[[,] KEEPNULLS] - Specifies the column should remain empty during a bulk import operation null value, without any default values for the inserted column
[[,] = KILOBYTES_PER_BATCH KILOBYTES_PER_BATCH]
[[,] = LASTROW LAST_ROW] - last line number to be loaded
[[,] MAXERRORS = max_errors] - Maximum number specifies the syntax error occurs in the data, this number will more than cancel the bulk import operation.
[[,] ORDER ({column [ASC | DESC]} [, ... n])] - data designating data file will be sorted
[[,] = ROWS_PER_BATCH ROWS_PER_BATCH]
[[,] = ROWTERMINATOR 'row_terminator' ] - separator line identification symbol
[[,] TABLOCK] - Specifies the duration of the import operation acquired a table level lock is a mass
[[,] ERRORFILE = 'file_name '] - Specifies and can not collect malformed converted to OLE DB rowset file line.
)]

A simple example

To compare the difference between ordinary and BULK INSERT inserted one by one, we have a simple example, to see the effect of the actual operation.

Step 1: Create two different database table, points table for the Student and Student1, table structures are identical, only the ID, NAME, AGE three simple fields.

SQL-- a daily solution to quickly insert ------ Bulk Insert

 

Step Two: Create a console program, through a simple cycle, data is written to generate 500,000 txt document, the key code is as follows:

/// <summary>
/// 生成测试数据
/// </summary>
private static void GenerateTestData()
{
string fileName = "sql";
int i = 1;
while (i <= 500000)
{
string strInsert = string.Format("{0},'test{0}',{0}|", i);
File.AppendText(strInsert, fileName);
i++;
}
}

The third step: a package two methods were used to perform bulk insert and insert general, specific code as follows:

/// <Summary> 
/// bulk insert test
/// </ Summary>
Private static void BulkInsertTest ()
{
String strFilePath @ = "D: \ Learning \ the ASP.NET \ QYH.BlukInsertTest \ sql.txt";
String = strTableName "Student";

/ * in each field information "," split
* to each of the data "|" delimited
* per 100,000 a transaction data * /
String SQL = string.Format ( "the BULK the INSERT 0 { the FROM} '{}. 1' the WITH (the FIELDTERMINATOR = ',', ROWTERMINATOR = '|', the BATCHSIZE = 50000) ", strTableName, strFilePath);
the DBHelper DBHelper the DBHelper new new = ();
dbHelper.Excute (SQL);

}

// / <Summary>
/// normal inserted test
/// </ Summary>
Private static void CommonInsertTest ()
{
int I =. 1;
while (i <= 500000)
{
string sqlInsert = string.Format("insert into Student1(id,Name,Age) values({0},'test{0}',{0})", i);
new DBHelper().Excute(sqlInsert);
i++;
}
}

Fourth Step: Main Main function calls and ordinary bulk insert insertion method, and by calculating the execution time Stopwatch, Pragram complete code as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QYH.BlukInsertTest.FileMange;
using QYH.BlukInsertTest.DataBase;
using System.Diagnostics;

namespace QYH.BlukInsertTest
{
class Program
{
static void Main(string[] args)
{
//用于生成海量数据
//GenerateTestData();

Stopwatch stopwatch = Stopwatch.StartNew();
try
{
BulkInsertTest();
}
catch (Exception)
{

//throw;
}

stopwatch.Stop();
string strResult = "批量插入耗时:" + stopwatch.ElapsedMilliseconds.ToString();

Stopwatch1 = Stopwatch.StartNew The Stopwatch ();
CommonInsertTest ();
stopwatch1.Stop ();
String str1Result = "Common Insert Processed:" + stopwatch1.ElapsedMilliseconds.ToString ();

String strTestResult = "Result";
File.WriteTextAsync (strResult + "\ R & lt \ n-" + str1Result, strTestResult);

//Console.Read ();
}

/// <Summary>
/// bulk insert test
/// </ Summary>
Private static void BulkInsertTest ()
{
String strFilePath = @ "D: \ learning \ ASP.NET \ QYH.BlukInsertTest \ sql.txt";
String strTableName = "Student";

/ * information for each field to "," split
* each piece of data to | delimiter ""
* 100 000 data per transaction * /
string sql = string.Format("BULK INSERT {0} FROM '{1}' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR ='|',BATCHSIZE = 50000)", strTableName, strFilePath);
DBHelper dbHelper = new DBHelper();
dbHelper.Excute(sql);

}

/// <summary>
/// 普通插入测试
/// </summary>
private static void CommonInsertTest()
{
int i = 1;
while (i <= 500000)
{
string sqlInsert = string.Format("insert into Student1(id,Name,Age) values({0},'test{0}',{0})", i);
new DBHelper().Excute(sqlInsert);
i++;
}
}

/// <summary>
/// 生成测试数据
/// </summary>
private static void GenerateTestData()
{
string fileName = "sql";

int i = 1;
while (i <= 500000)
{
string strInsert = string.Format("{0},'test{0}',{0}|", i);
File.AppendText(strInsert, fileName);
i++;
}
}
}
}

Example also uses two helper classes, DBHelper.cs and File.cs, since only for demonstration, so writing is very simple, where the file path is hard-coded, and can be replaced by the actual path.

DBHelper.cs

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QYH.BlukInsertTest.DataBase
{
public class DBHelper
{
public string connectionString = "Server=.;Database=QYHDB;User ID=sa;Password=123456;Trusted_Connection=False;";

public void Excute(string sql)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
command.CommandTimeout = 0;
command.Connection = conn;
command.CommandText = sql;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}

File.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QYH.BlukInsertTest.FileMange
{
public class File
{
public static string strFilePath = @"D:\学习\ASP.NET\QYH.BlukInsertTest";

public static async void WriteTextAsync(string text, string fileName)
{
using (StreamWriter outputFile = new StreamWriter(strFilePath + @"\" + fileName + ".txt"))
{
await outputFile.WriteAsync(text);
}
}

public static void AppendText(string text, string fileName)
{
// Append text to an existing file named "WriteLines.txt".
using (StreamWriter outputFile = new StreamWriter(strFilePath + @"\" + fileName + ".txt",true))
{
outputFile.WriteLine(text);
}
}
}
}

Everything is ready, start running, as follows:

SQL-- a daily solution to quickly insert ------ Bulk Insert

 

Wherein units of milliseconds , we can see from the results 500,000 BULK INSER inserted data need not three seconds , whereas the ordinary one by inserting it takes more than 20 minutes

Guess you like

Origin www.cnblogs.com/twelvezuo/p/11671205.html