目录

1.前言

2.BULK INSERT

3.简单示例

前言

  由于昨天接到一个客户反馈导出数据卡死的问题,于是决定今天模拟一下千万级的数据,然后傻傻的等待插入数据了半天......

  对于海量数据,上百万上千万的数据插入,我们用ADO.NET提供的普通一条一条数据插入非常非常慢,好在Sql Server为我们提供了批量插入方法。

BULK INSERT

  语法

  

  主要参数说明

  database_name

  指定的表或视图所在的数据库的名称,如果未指定,则默认为当前数据库。

  schema_name

  表或视图架构的名称。

  table_name

  要将数据大容量导入其中的表或视图的名称。

  ‘data_file’

  数据文件的完整路径,该数据文件包含到导入到指定表或视图中的数据。使用BULK INSERT可以从磁盘导入数据。

  BATCHSIZE=batch_size

  指定批量处理中的行数。每个批处理作为一个事物复制到服务器。

  CHECK_CONSTRAINTS
      指定在大容量导入操作期间,必须检查所有对目标表或视图的约束。

  FIELDTERMINATOR ='field_terminator'
  指定要用于 char 和 widechar 数据文件的字段终止符,即字段的分隔符。 默认的字段终止符是 \t(制表符)。

  ROWTERMINATOR ='row_terminator'
  指定要用于 char 和 widechar 数据文件的行终止符,即行的分隔符。

  

  更多参数说明,请参考: https://msdn.microsoft.com/zh-cn/library/ms188365.aspx

简单示例

  为了对比BULK INSERT和普通逐条插入的差异,我们通过一个简单的示例,通过实际运行来查看效果。  

  第一步:在数据库新建两张一样的表,分表为Student和Student1,表结构完全相同,只有ID,NAME,AGE三个简单的字段。

  

  第二步:新建一个控制台程序,通过一个简单的循环,生成500000条数据写入到txt文件中,关键代码如下:  

 /// <summary>
/// 生成测试数据
/// </summary>
private static void GenerateTestData()
{
string fileName = "sql"; int i = ;
while (i <= )
{
string strInsert = string.Format("{0},'test{0}',{0}|", i);
File.AppendText(strInsert, fileName);
i++;
}
}

  第三步:封装出两个方法,分别用来执行批量插入和普通插入,具体代码如下:

/// <summary>
/// 批量插入测试
/// </summary>
private static void BulkInsertTest()
{
string strFilePath = @"D:\学习\ASP.NET\QYH.BlukInsertTest\sql.txt";
string strTableName = "Student"; /* 每一个字段的信息以“,”分割
*每一条数据以“|”符号分隔
* 每10万条数据一个事务*/
string sql = string.Format("BULK INSERT {0} FROM '{1}' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR ='|',BATCHSIZE = 50000)", strTableName, strFilePath);
DBHelper dbHelper = new DBHelper();
dbHelper.Excute(sql); } /// <summary>
/// 普通插入测试
/// </summary>
private static void CommonInsertTest()
{
int i = ;
while (i <= )
{
string sqlInsert = string.Format("insert into Student1(id,Name,Age) values({0},'test{0}',{0})", i);
new DBHelper().Excute(sqlInsert);
i++;
}
}

  第四步:Main主函数中调用批量插入和普通插入方法,并通过Stopwatch计算出执行时间,Pragram完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QYH.BlukInsertTest.FileMange;
using QYH.BlukInsertTest.DataBase;
using System.Diagnostics; namespace QYH.BlukInsertTest
{
class Program
{
static void Main(string[] args)
{
//用于生成海量数据
//GenerateTestData(); Stopwatch stopwatch = Stopwatch.StartNew();
try
{
BulkInsertTest();
}
catch (Exception)
{ //throw;
} stopwatch.Stop();
string strResult = "批量插入耗时:" + stopwatch.ElapsedMilliseconds.ToString(); Stopwatch stopwatch1 = Stopwatch.StartNew();
CommonInsertTest();
stopwatch1.Stop();
string str1Result = "普通插入耗时:" + stopwatch1.ElapsedMilliseconds.ToString(); string strTestResult = "result";
File.WriteTextAsync(strResult + "\r\n" + str1Result, strTestResult); //Console.Read();
} /// <summary>
/// 批量插入测试
/// </summary>
private static void BulkInsertTest()
{
string strFilePath = @"D:\学习\ASP.NET\QYH.BlukInsertTest\sql.txt";
string strTableName = "Student"; /* 每一个字段的信息以“,”分割
*每一条数据以“|”符号分隔
* 每10万条数据一个事务*/
string sql = string.Format("BULK INSERT {0} FROM '{1}' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR ='|',BATCHSIZE = 50000)", strTableName, strFilePath);
DBHelper dbHelper = new DBHelper();
dbHelper.Excute(sql); } /// <summary>
/// 普通插入测试
/// </summary>
private static void CommonInsertTest()
{
int i = ;
while (i <= )
{
string sqlInsert = string.Format("insert into Student1(id,Name,Age) values({0},'test{0}',{0})", i);
new DBHelper().Excute(sqlInsert);
i++;
}
} /// <summary>
/// 生成测试数据
/// </summary>
private static void GenerateTestData()
{
string fileName = "sql"; int i = ;
while (i <= )
{
string strInsert = string.Format("{0},'test{0}',{0}|", i);
File.AppendText(strInsert, fileName);
i++;
}
}
}
}

  示例中还用到两个辅助类,DBHelper.cs和File.cs,由于仅用于演示,所以写的非常简单,其中文件路径是写死的,可以替换成实际路径。

  DBHelper.cs  

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace QYH.BlukInsertTest.DataBase
{
public class DBHelper
{
public string connectionString = "Server=.;Database=QYHDB;User ID=sa;Password=123456;Trusted_Connection=False;"; public void Excute(string sql)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
command.CommandTimeout = ;
command.Connection = conn;
command.CommandText = sql;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}

   File.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace QYH.BlukInsertTest.FileMange
{
public class File
{
public static string strFilePath = @"D:\学习\ASP.NET\QYH.BlukInsertTest"; public static async void WriteTextAsync(string text, string fileName)
{
using (StreamWriter outputFile = new StreamWriter(strFilePath + @"\" + fileName + ".txt"))
{
await outputFile.WriteAsync(text);
}
} public static void AppendText(string text, string fileName)
{
// Append text to an existing file named "WriteLines.txt".
using (StreamWriter outputFile = new StreamWriter(strFilePath + @"\" + fileName + ".txt",true))
{
outputFile.WriteLine(text);
}
}
}
}

  

  一切准备就绪,开始运行,结果如下:

  

  其中单位为毫秒,从结果我们可以看出BULK INSER插入500000条数据还不需要3秒,而普通逐条插入却需要20多分钟

Sql Server海量数据插入的更多相关文章

  1. SQL Server 批量插入数据的两种方法

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍 SQL Server支持的两种批 ...

  2. SQL Server 批量插入数据的两种方法(转)

    此文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx 在SQL Server 中插入一条 ...

  3. 转:SQL Server 批量插入数据的两种方法

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  4. ASP.NET MVC与Sql Server交互, 插入数据

    在"ASP.NET MVC与Sql Server建立连接"中,与Sql Server建立了连接.本篇实践向Sql Server中插入数据. 在数据库帮助类中增加插入数据的方法. p ...

  5. sql server不要插入大数据,开销太大

    sql server或者说关系型数据库中不要做一个字段存储大数据量的设计,比如要插入3000w条数据,然后每条数据中有一个文章字段,这个字段每条大概都需要存储几m的数据,那么算下来这个表就得有几百个G ...

  6. SQL Server返回插入数据的ID和受影响的行数

    首先看看数据库里面的数据(S_Id为自增长标识列): sql server 中返回上一次插入数据的ID(标识值)有三种方式: 第一种 @@IDENTITY: insert into Student(S ...

  7. SQL Server 批量插入数据方案 SqlBulkCopy 的简单封装,让批量插入更方便

    一.Sql Server插入方案介绍 关于 SqlServer 批量插入的方式,有三种比较常用的插入方式,Insert.BatchInsert.SqlBulkCopy,下面我们对比以下三种方案的速度 ...

  8. identity in sql server 批量插入history

    https://stackoverflow.com/questions/1920558/what-is-the-difference-between-scope-identity-identity-i ...

  9. 连接sql server、插入数据、从数据库获取时间(C#)

    using System; using System.Data.SqlClient; namespace Test { //连接数据库 public class Connection { privat ...

随机推荐

  1. Windows无法安装到GPT分区的磁盘的解决方法

    thinkpad 预装win8的机子,硬盘采用gpt分区,在重新安装其它系统的时候是无法安装的,会提示“windows无法安装到这个磁盘,选中的磁盘采用GPT分区 形式".所以先采用下面的方 ...

  2. Arch Linux中文乱码解决

    Arch Linux中文乱码解决 1.安装中文字体 pacman -S wqy-zenhei ttf-fireflysung (flash乱码)   ---乱码的原因就是缺少中文字体的支持,下载文泉驿 ...

  3. ORA-20011 ORA-29913 and ORA-29400 with Associated KUP-XXXXX Errors from DBMS_STATS.GATHER_STATS_JOB(Doc ID 1274653.1)

    首先在alert log裡面頻繁的看見如下錯誤: DBMS_STATS: GATHER_STATS_JOB encountered errors.  Check the trace file. Err ...

  4. 在Jenkins中获取GitHub对应Repository的Resource Code

    1):Install Jenkins 请看如下链接: https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins 2):Install ...

  5. Microchip微芯HCS301解密HCS360解密HCS361芯片解密多少钱?

    Microchip微芯HCS系列芯片解密型号: HCS101 | HCS200 | HCS201 | HCS201T | HCS300 | HCS300T HCS301 | HCS301T | HCS ...

  6. call()与apply()

    1.obj1.method1.call(obj2,argument1,argument2) call的作用就是把obj1的方法放到obj2上使用 2. add 来替换 sub,add.call(sub ...

  7. 创建DOTA2本地数据库(一)

    在APP中,用本地数据库好于频繁的联网去获取相关数据.我使用SQLite作为本地的数据库,比较轻巧. 英雄 首先先建立英雄的数据库,暂时我先只设置ID,name,loaclized_name这三种,我 ...

  8. c语言指针疑惑[转载]

    c99的动态数组是在栈上面开辟的,而new出来的是在堆上面开辟的.栈和堆的地址是从两端相向增长的.栈很小,一般只有几十k,vc6好像是64k.堆很大,在win32的虚拟地址空间可以分配到2g的内存.栈 ...

  9. 【DP】HDU 1087

    HDU 1078 Super Jumping! Jumping! Jumping! 题意: 有这么个游戏,从start到end(自己决定在哪停下来)连续跳圈,中间不能空一个圈不跳,圈里的数字必须比你上 ...

  10. ZeroMQ接口函数之 :zmq_ctx_set - 设置环境上下文属性

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_set zmq_ctx_set(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ct ...