视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
.NET批量大数据插入性能分析及比较(6.使用表值参数)
2020-11-09 08:07:20 责编:小采
文档


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

代码如下:

结果如下:

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.使用表值参数)

下载本文
显示全文
专题