表值參數(Table-valued Parameter)是SQL Server 2008增加的新特性,可以將DataTable做爲參數傳遞給存儲過程。
數據庫執行腳本如下
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
代碼如下:
結果如下:
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;
從時間上來看,似乎並不必前面的案例強,但批處理量得增加,寫性能在持續提高,而且實際上程序中花費了大量的時間在創建DataTable及填充其數據上面,如果傳遞給函數的就是一個DataTable集合,相信使用表值參數的表現會更好。
但考慮到需要爲插入的表創建類型,創建存儲過程,個人認爲其通用性不是很好
全文鏈接:
.NET批量大數據插入性能分析及比較(2.普通插入與拼接sql批量插入)
.NET批量大數據插入性能分析及比較(4.使用DataAdapter批量插入)