数据插入使用了以下几种方式

1. 逐条数据插入
2. 拼接sql语句批量插入
3. 拼接sql语句并使用Transaction
4. 拼接sql语句并使用SqlTransaction
5. 使用DataAdapter
6. 使用TransactionScope及SqlBulkCopy
7. 使用表值参数

数据库使用SQL Server,脚本如下

create table TestTable
(
Id int
,Name nvarchar(20)
)

程序中生成测试DataTable结构和测试数据的类如下

[c-sharp] view plaincopyprint?
1.public class Tools 
2.{ 
3.    public static DataTable MakeDataTable() 
4.    { 
5.        DataTable table = new DataTable(); 
6. 
7.        //生成DataTable的模式(schema)  
8.        table.Columns.Add("Id", Type.GetType("System.Int32")); 
9.        table.Columns.Add("Name", Type.GetType("System.String")); 
10. 
11.        //设置主键  
12.        table.PrimaryKey = new DataColumn[] { table.Columns["ID"] }; 
13.        table.Columns["Id"].AutoIncrement = true; 
14.        table.Columns["Id"].AutoIncrementSeed = 1; 
15.        table.Columns["Id"].ReadOnly = true; 
16.        return table; 
17.    } 
18. 
19.    public static void MakeData(DataTable table, int count) 
20.    { 
21.        if (table == null) 
22.            return; 
23. 
24.        if (count <= 0) 
25.            return; 
26. 
27.        DataRow row = null; 
28. 
29.        for (int i = 1; i <= count; i++) 
30.        { 
31.            //创建一个新的DataRow对象(生成一个新行)  
32.            row = table.NewRow(); 
33.            row["Name"] = "Test" + i.ToString(); 
34.            //添加新的DataRow  
35.            table.Rows.Add(row); 
36.        } 
37.    } 
38.} 
    public class Tools
    {
        public static DataTable MakeDataTable()
        {
            DataTable table = new DataTable();

//生成DataTable的模式(schema)
            table.Columns.Add("Id", Type.GetType("System.Int32"));
            table.Columns.Add("Name", Type.GetType("System.String"));

//设置主键
            table.PrimaryKey = new DataColumn[] { table.Columns["ID"] };
            table.Columns["Id"].AutoIncrement = true;
            table.Columns["Id"].AutoIncrementSeed = 1;
            table.Columns["Id"].ReadOnly = true;
            return table;
        }

public static void MakeData(DataTable table, int count)
        {
            if (table == null)
                return;

if (count <= 0)
                return;

DataRow row = null;

for (int i = 1; i <= count; i++)
            {
                //创建一个新的DataRow对象(生成一个新行)
                row = table.NewRow();
                row["Name"] = "Test" + i.ToString();
                //添加新的DataRow
                table.Rows.Add(row);
            }
        }
    }

使用Log4net记录日志,默认插入记录数为40000条,每次插入1条,可在界面修改,使用System.Diagnostics.StopWatch记录插入时间,每次测试后删除原表重建

窗体代码如下:

  1. public delegate bool InsertHandler(DataTable table, int batchSize);
  2. public partial class FrmBatch : Form
  3. {
  4. private Stopwatch _watch = new Stopwatch();
  5. public FrmBatch()
  6. {
  7. InitializeComponent();
  8. }
  9. private void FrmBatch_Load(object sender, EventArgs e)
  10. {
  11. txtRecordCount.Text = "40000";
  12. txtBatchSize.Text = "1";
  13. }
  14. //逐条数据插入
  15. private void btnInsert_Click(object sender, EventArgs e)
  16. {
  17. Insert(DbOperation.ExecuteInsert, "Use SqlServer Insert");
  18. }
  19. //拼接sql语句插入
  20. private void btnBatchInsert_Click(object sender, EventArgs e)
  21. {
  22. Insert(DbOperation.ExecuteBatchInsert, "Use SqlServer Batch Insert");
  23. }
  24. //拼接sql语句并使用Transaction
  25. private void btnTransactionInsert_Click(object sender, EventArgs e)
  26. {
  27. Insert(DbOperation.ExecuteTransactionInsert, "Use SqlServer Batch Transaction Insert");
  28. }
  29. //拼接sql语句并使用SqlTransaction
  30. private void btnSqlTransactionInsert_Click(object sender, EventArgs e)
  31. {
  32. Insert(DbOperation.ExecuteSqlTransactionInsert, "Use SqlServer Batch SqlTransaction Insert");
  33. }
  34. //使用DataAdapter
  35. private void btnDataAdapterInsert_Click(object sender, EventArgs e)
  36. {
  37. Insert(DbOperation.ExecuteDataAdapterInsert, "Use SqlServer DataAdapter Insert");
  38. }
  39. //使用TransactionScope
  40. private void btnTransactionScopeInsert_Click(object sender, EventArgs e)
  41. {
  42. Insert(DbOperation.ExecuteTransactionScopeInsert, "Use SqlServer TransactionScope Insert");
  43. }
  44. //使用表值参数
  45. private void btnTableTypeInsert_Click(object sender, EventArgs e)
  46. {
  47. Insert(DbOperation.ExecuteTableTypeInsert, "Use SqlServer TableType Insert");
  48. }
  49. private DataTable InitDataTable()
  50. {
  51. DataTable table = Tools.MakeDataTable();
  52. int count = 0;
  53. if (int.TryParse(txtRecordCount.Text.Trim(), out count))
  54. {
  55. Tools.MakeData(table, count);
  56. //MessageBox.Show("Data Init OK");
  57. }
  58. return table;
  59. }
  60. public void Insert(InsertHandler handler, string msg)
  61. {
  62. DataTable table = InitDataTable();
  63. if (table == null)
  64. {
  65. MessageBox.Show("DataTable is null");
  66. return;
  67. }
  68. int recordCount = table.Rows.Count;
  69. if (recordCount <= 0)
  70. {
  71. MessageBox.Show("No Data");
  72. return;
  73. }
  74. int batchSize = 0;
  75. int.TryParse(txtBatchSize.Text.Trim(), out batchSize);
  76. if (batchSize <= 0)
  77. {
  78. MessageBox.Show("batchSize <= 0");
  79. return;
  80. }
  81. bool result = false;
  82. _watch.Reset(); _watch.Start();
  83. result = handler(table, batchSize);
  84. _watch.Stop(www.nuoya66.com);
  85. string log = string.Format("{0};RecordCount:{1};BatchSize:{2};Time:{3};", msg, recordCount, batchSize, _watch.ElapsedMilliseconds);
  86. LogHelper.Info(log);
  87. MessageBox.Show(result.ToString());
  88. }
  89. }

.NET批量大数据插入性能分析及比较的更多相关文章

  1. 大数据应用之HBase数据插入性能优化实测教程

    引言: 大家在使用HBase的过程中,总是面临性能优化的问题,本文从HBase客户端参数设置的角度,研究HBase客户端数据批量插入性能优化的问题.事实胜于雄辩,数据比理论更有说服力,基于此,作者设计 ...

  2. Impala简介PB级大数据实时查询分析引擎

    1.Impala简介 • Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. • 基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 ...

  3. .NET 百万级 大数据插入、更新 ,支持多种数据库

    功能介绍  (需要版本5.0.44) 大数据操作ORM性能瓶颈在实体转换上面,并且不能使用常规的Sql去实现 当列越多转换越慢,SqlSugar将转换性能做到极致,并且采用数据库最佳API 操作数据库 ...

  4. sql 根据指定条件获取一个字段批量获取数据插入另外一张表字段中+MD5加密

    /****** Object: StoredProcedure [dbo].[getSplitValue] Script Date: 03/13/2014 13:58:12 ******/ SET A ...

  5. 学习Hadoop+Spark大数据巨量分析与机器学习整合开发-windows利用虚拟机实现模拟多节点集群构建

    记录学习<Hadoop+Spark大数据巨量分析与机器学习整合开发>这本书. 第五章 Hadoop Multi Node Cluster windows利用虚拟机实现模拟多节点集群构建 5 ...

  6. "Entity Framework数据插入性能追踪"读后总结

    园友莱布尼茨写了一篇<Entity Framework数据插入性能追踪>的文章,我感觉不错,至少他提出了问题,写了出来,引起了大家的讨论,这就是一个氛围.读完文章+评论,于是我自己也写了个 ...

  7. 向mysql中批量插入数据的性能分析

    MYSQL批量插入数据库实现语句性能分析 假定我们的表结构如下 代码如下   CREATE TABLE example (example_id INT NOT NULL,name VARCHAR( 5 ...

  8. 大数据应用之HBase数据插入性能优化之多线程并行插入测试案例

    一.引言: 上篇文章提起关于HBase插入性能优化设计到的五个参数,从参数配置的角度给大家提供了一个性能测试环境的实验代码.根据网友的反馈,基于单线程的模式实现的数据插入毕竟有限.通过个人实测,在我的 ...

  9. 使用Oracle Stream Analytics 21步搭建大数据实时流分析平台

    概要: Oracle Stream Analytics(OSA)是企业级大数据流实时分析计算平台.它可以通过使用复杂的关联模式,扩充和机器学习算法来自动处理和分析大规模实时信息.流式传输的大数据可以源 ...

随机推荐

  1. .net 网站发布 Web.Config中的<compilation debug="true"/>

    Web.Config中的<compilation debug="true"/> <compilation debug="true"/> ...

  2. (转).net程序员转战android第三篇---登录模块之静态登录

    这一篇我将分2个部分记录登录界面,第一部分是静态登录, 这部分将如何从界面布局.控件使用.文件关系.数据验证.登陆实现等5小块记录. 第二部分是动态登录,这块会基于上面的4小块,在数据验证不是静态数据 ...

  3. UIView /  UIView的布局

    //! 一个视图可以有n个子视图,但是一个视图只能有一个父视图 struct CGRect {   CGPoint origin;   CGSize size; }; CGRectMake(CGFlo ...

  4. No2_2.接口继承多态_Java学习笔记_继承

    ***类的继承***1.继承的实现extends2.[修饰符] class 子类名 extends 父类名{}3.修饰符一般为:public abstract final4.重写是指父子类之间的关系, ...

  5. #能力开放平台系列-Fiddler访问Rest服务

    问题 最近开发能力开放平台,需要将Dubbo服务转换成Rest服务,虽然转换很成功(后续文档会写出如何将Dubbo服务转换成Rest接口),但是调试起来特别的麻烦. 解决方案: Fiddler解决方案 ...

  6. Kendo Web UI Grid里时间格式转换

    我和大家分享一下我的kendo的学习心得.如果不好的地方多多包含或者给我留言请看图 kendo里时间格式如图Oder Date列里的格式.但是我们想把时间转换成中国人习惯的格式.如Shipped Da ...

  7. Core Data入门-备用

    简介 Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象. ...

  8. 一条执行4秒的sql语句导致的系统问题 (转)

    为了一看究竟,抓取了一个awr报告.发现系统的负载情况确实很严重,每秒的redo有1.6M,可见系统的负载不是主要在select上,可能有一些dml之类的操作极为频繁. 看了下等待事件.都是关于loc ...

  9. 【转】android cts failed items

    原文网址:http://blog.csdn.net/linsa0517/article/details/19031479 Fail的一些修改   1.直接设置问题 estUnknownSourcesO ...

  10. margin:0 auto 与 text-align:center 的区别(转载)

    摘自:http://www.cnblogs.com/zhwl/p/3529473.html 基本概念: 1.text-align: 属性规定元素中的文本的水平对齐方式;   该属性通过指定行框与哪个点 ...