好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

.NET批量大数据插入性能分析及比较(6.使用表值参数)

表值 参数 (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 TestT

表值 参数 (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

代码如下:

#region 使用 表值 参数 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 = CommandType.StoredProcedure; cmd.CommandText = "InsertData"; cn.Open(); for (int i = 0; i

结果如下:

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批量大 数据 插入 性能 分析 及 比较 (1.准备工作)

.NET批量大 数据 插入 性能 分析 及 比较 (2.普通 插入 与拼接sql批量 插入 )

.NET批量大 数据 插入 性能 分析 及 比较 (3. 使用 事务)

.NET批量大 数据 插入 性能 分析 及 比较 (4. 使用 DataAdapter批量 插入 )

.NET批量大 数据 插入 性能 分析 及 比较 (5. 使用 SqlBulkCopy)

.NET批量大 数据 插入 性能 分析 及 比较 (6. 使用 表值 参数 )

查看更多关于.NET批量大数据插入性能分析及比较(6.使用表值参数)的详细内容...

  阅读:36次