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

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. javascriptt切换组件MyTab.js封装

    之前做的大多数是jquery的插件,就优雅性来说,我觉得还是原生的代码,写起来更舒服一点,虽然麻烦很多. 之前写了一个利用完美运动框架的轮播效果,因为使用的是原生的代码,因为不懂原生对象封装的原因一直 ...

  2. javascript紧接上一张for循环的问题,我自己的理解

    这类问题,通常都是在for循环里,根据i的变化作为dom的下标来作当前dom的点击事件, 预期是,每个点击事件都对应相应的i下标,但是问题是,每次点击的都是最后一次节点的dom. 原因就是其实我们作点 ...

  3. Base64加密解密

    /// <summary> /// 实现Base64加密解密 /// </summary> public sealed class Base64 { /// <summa ...

  4. Oracle内链接+外连接详解

    inner join(内连接) 内连接也称为等同连接,返回的结果集是两个表中所有相匹配的数据,而舍弃不匹配的数据.也就是说,在这种查询中,DBMS只返回来自源表中的相关的行,即查询的结果表包含的两源表 ...

  5. C#属性和readonly类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 给定范围内产生N个不同的随机数

    void RandNumbs(int nLimts, int result[], int n)//给定范围内产生n个不同随机数(1-nLimts),并存储到result中 { int nNum = 0 ...

  7. ORA-00845: MEMORY_TARGET not supported

    Enabling Automatic Memory Management alter system set memory_max_target=50G scope=spfile; alter syst ...

  8. 解决Sublime-Text-3在ubuntu下中文输入的问题

    在ubuntu下使用ST这神器已经一段日子了,但是一直有个纠结的问题,就是中文输入非常坑爹,曾经一段时间,使用inputHelper这个插件来解决, 但是……每次都要按个快捷键,弹出一个小小小框来输入 ...

  9. Virtual Box 工具栏(菜单栏)消失的解决方法

    异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html 现在Virtual Box非常牛逼(不排除Oracle又准备像Java SE那样 ...

  10. 使用QtCreator作为ROS调试器

    如果你用过QtCreator,你一定会喜欢上它. 流畅的速度,强大的功能,优雅的外观,友好的界面,一切让人如此舒服.而且它支持从命令行作为调试器启动,只需加上-debug exe即可. 因此我想如果能 ...