Table-valuedParameter is a new feature added by SQLServer2008. You can pass a able as a parameter to the stored procedure. The database execution script is as follows: CREATETYPETestTypeASTABLE (IdintNOTNULL, Namenvarchar (20) NOTNULL) CREATEPROCInsertData @ rowsTestT
Table-valued Parameter is a new feature added to SQL Server 2008. You can pass a able as a Parameter to the stored procedure. The execution script of the data library is AS follows: create type TestType as table (Id int not null, Name nvarchar (20) not null) create proc InsertData @ rows TestT
Table valueParameters(Table-valued Parameter) is a new feature added by SQL Server 2008. You can use DataTableParametersTo the stored procedure.
DataThe library execution script is as follows:
Create type TestType AS TABLE
(
Id int NOT NULL
, Name nvarchar (20) NOT NULL
)
Create proc InsertData
@ Rows TestType READONLY
As
Begin
Set nocount on
Insert into TestTable (Id, Name)
Select Id, Name from @ rows
End
The Code is as follows:
# Region <strong> use </strong> Table value <strong> parameter </strong> public static bool ExecuteTableTypeInsert (DataTable dt, int batchSize) {int count = dt. rows. count; bool flag = false; SqlConnection cn = null; SqlCommand cmd = null; DataTable tempTable = Tools. makeDataTable (); DataRow row = null; try {cn = new SqlConnection (connectionString); cmd = new SqlCommand (); cmd. connection = cn; cmd. commandType = CommandTy Pe. storedProcedure; cmd. commandText = "InsertData"; cn. open (); for (int I = 0; I <count; I + = batchSize) {for (int j = I; j <I + batchSize & j <count; j ++) {row = tempTable. newRow (); row ["Id"] = dt. rows [j] ["Id"]; row ["Name"] = dt. rows [j] ["Name"]; tempTable. rows. add (row);} SqlParameter param = cmd. parameters. addWithValue ("@ rows", tempTable); param. sqlDbType = SqlDbType. structured; param. type Name = "TestType"; cmd. executeNonQuery (); tempTable. clear (); cmd. parameters. clear ();} flag = true;} catch (Exception ex) {LogHelper. error (ex. message); return false;} finally {if (cn! = Null) {if (cn. State = ConnectionState. Open) {cn. Close () ;}cn. Dispose () ;}if (cmd! = Null) cmd. Dispose ();} return flag ;}# endregion
The result is as follows:
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 10; Time: 15312;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 20; Time: 7806;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 50; Time: 3767;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 100; Time: 2217;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 200; Time: 1743;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 400; Time: 1575;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 500; Time: 1566;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 600; Time: 1374;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 700; Time: 1286;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 800; Time: 1463;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 1000; Time: 1272;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 2000; Time: 1069;
Use SqlServer TableType Insert; RecordCount: 40000; BatchSize: 4000; Time: 1001;
From the time point of view, it seems that the previous case does not have to be strong, but the number of batch processing has increased, writePerformanceIn fact, the program spends a lot of time creating a able and filling itDataAs shown in the preceding figure, if a DataTable set is passed to the functionUseTable valueParametersThe performance will be better.
However, considering the needInsertTable creation type and storage process, I personally think it is not very universal
Full text link:
. NET large batchesDataInsertPerformanceAnalysisAndComparison(1. Preparation)
. NET large batchesDataInsertPerformanceAnalysisAndComparison(2. NormalInsertBatch join SQLInsert)
. NET large batchesDataInsertPerformanceAnalysisAndComparison(3.UseTransaction)
. NET large batchesDataInsertPerformanceAnalysisAndComparison(4.UseDataAdapter batchInsert)
. NET large batchesDataInsertPerformanceAnalysisAndComparison(5.UseSqlBulkCopy)
. NET large batchesDataInsertPerformanceAnalysisAndComparison(6.UseTable valueParameters)