大数据批量插入小练习_SqlServer
生活随笔
收集整理的這篇文章主要介紹了
大数据批量插入小练习_SqlServer
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這幾天把sqlserver批量插入也整理了一下,性能方面有很大的提高,下面直接上代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
namespace FromSt
{
? ?public static class Run_From
? ?{
? ? ? ?private static int dd = 0;
? ? ? ?public static void InsertForTest(object ss)
? ? ? ?{
? ? ? ? ? ?InsertData(MakeDT(int.Parse(ss.ToString())));
? ? ? ? ? ?//Thread st = new Thread(WriterLog);
? ? ? ? ? ?//st.Start();
? ? ? ?}
? ? ? ?public static void WriterLog()
? ? ? ?{
? ? ? ? ? ?string ss = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + (dd + 1);
? ? ? ? ? ?using (StreamWriter sw = new StreamWriter(@"d:\wService.txt", true, Encoding.GetEncoding("utf-8"), ss.Length))
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sw.WriteLine(ss);
? ? ? ? ? ?}
? ? ? ?}
//創建表
? ? ? ?public static DataTable MakeDT(int count)
? ? ? ?{
? ? ? ? ? ?DataTable dt = new DataTable();
? ? ? ? ? ?//DataColumn dc = new DataColumn ("name1",Type.GetType("System.String"));
? ? ? ? ? ?dt.Columns.AddRange(new DataColumn[]{
? ? ? ? ? new DataColumn("name",Type.GetType("System.String")),
? ? ? ? ?});
? ? ? ? ? ?// dt.Columns.Add(dc);
? ? ? ? ? ?Stopwatch sw = new Stopwatch();
? ? ? ? ? ?sw.Start();
? ? ? ? ? ?for (int i = 0; i < count; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?DataRow dr = dt.NewRow();
? ? ? ? ? ? ? ?dr["name"] = "zl" + i;
? ? ? ? ? ? ? ?dt.Rows.Add(dr);
? ? ? ? ? ?}
? ? ? ? ? ?sw.Stop();
? ? ? ? ? ?double dd = sw.Elapsed.TotalSeconds;
? ? ? ? ? ?return dt;
? ? ? ?}
? ? ? ?public static void InsertData(DataTable dt)
? ? ? ?{
? ? ? ? ? ?string config = ConfigurationManager.ConnectionStrings["Sql2012"].ConnectionString;
? ? ? ? ? ?try
? ? ? ? ? ?{
? ? ? ? ? ? ? ?Stopwatch sw = new Stopwatch();
? ? ? ? ? ? ? ?sw.Start();
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?SqlBulkCopy sbc = new SqlBulkCopy(config, SqlBulkCopyOptions.UseInternalTransaction);
? ? ? ? ? ? ? ? ? ?sbc.BulkCopyTimeout = 500;//超時時間默認30秒鐘
? ? ? ? ? ? ? ? ? ?sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnRowsCopied); //處理完成指定行數后觸發的事件;這里不是必須的;
? ? ? ? ? ? ? ? ? ?sbc.NotifyAfter = dt.Rows.Count; //生成通知事件前要處理的行數
? ? ? ? ? ? ? ? ? ?sbc.DestinationTableName = "dbo.ss"; //對應數據庫的表名
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?sw.Stop();
? ? ? ? ? ? ? ?double dd = sw.Elapsed.TotalSeconds;
? ? ? ? ? ?}
? ? ? ? ? ?catch (Exception ex)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?throw ex;
? ? ? ? ? ?}
? ? ? ?}
? ? //這里實際是可以不用的,但是為了說明用法,我就寫了個方法
? ? ? ?private static void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
? ? ? ?{
? ? ? ? ? ?string ss = "";
? ? ? ? ? ?ss += args.RowsCopied.ToString() + " rows are copied<Br>";
? ? ? ?}
? ?}
}
-- 下面是逐次向表中插入數據的的用時前面表示的條數后面是秒數;注釋中的數據表示表中已有數據
select 1000000/16 ?-- 0 ?1個字段
select 1000000/7.90 -- 一百萬 ?一個字段
select 1000000/8.97 -- 二百萬 ?一個字段
select 1000000/7.60 -- 三百萬 ?一個字段
select 1000000/6.54 -- 4百萬 ? 一個字段
select 1000000/10.3626133 ?-- 五百萬 ?兩個字段
select 1000000/8.7146654 ?-- 六百萬 ?兩個字段
select 1000000/10.2771326 ?-- 八百萬 ?兩個字段
select 1000000/10.2771326 ?-- 九百萬 ? 兩個字段
select 1000000/7.4966066 ?-- 一千萬 ?兩個字段
需要注意下面的事項:
如果在創建表的時候如下面所示,id是自增列有默認值得話,插入式正常的
create(id int identity(1,1)not null,name varchar(50),sex varchar(20));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
namespace FromSt
{
? ?public static class Run_From
? ?{
? ? ? ?private static int dd = 0;
? ? ? ?public static void InsertForTest(object ss)
? ? ? ?{
? ? ? ? ? ?InsertData(MakeDT(int.Parse(ss.ToString())));
? ? ? ? ? ?//Thread st = new Thread(WriterLog);
? ? ? ? ? ?//st.Start();
? ? ? ?}
? ? ? ?public static void WriterLog()
? ? ? ?{
? ? ? ? ? ?string ss = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + (dd + 1);
? ? ? ? ? ?using (StreamWriter sw = new StreamWriter(@"d:\wService.txt", true, Encoding.GetEncoding("utf-8"), ss.Length))
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sw.WriteLine(ss);
? ? ? ? ? ?}
? ? ? ?}
//創建表
? ? ? ?public static DataTable MakeDT(int count)
? ? ? ?{
? ? ? ? ? ?DataTable dt = new DataTable();
? ? ? ? ? ?//DataColumn dc = new DataColumn ("name1",Type.GetType("System.String"));
? ? ? ? ? ?dt.Columns.AddRange(new DataColumn[]{
? ? ? ? ? new DataColumn("name",Type.GetType("System.String")),
? ? ? ? ?});
? ? ? ? ? ?// dt.Columns.Add(dc);
? ? ? ? ? ?Stopwatch sw = new Stopwatch();
? ? ? ? ? ?sw.Start();
? ? ? ? ? ?for (int i = 0; i < count; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?DataRow dr = dt.NewRow();
? ? ? ? ? ? ? ?dr["name"] = "zl" + i;
? ? ? ? ? ? ? ?dt.Rows.Add(dr);
? ? ? ? ? ?}
? ? ? ? ? ?sw.Stop();
? ? ? ? ? ?double dd = sw.Elapsed.TotalSeconds;
? ? ? ? ? ?return dt;
? ? ? ?}
? ? ? ?public static void InsertData(DataTable dt)
? ? ? ?{
? ? ? ? ? ?string config = ConfigurationManager.ConnectionStrings["Sql2012"].ConnectionString;
? ? ? ? ? ?try
? ? ? ? ? ?{
? ? ? ? ? ? ? ?Stopwatch sw = new Stopwatch();
? ? ? ? ? ? ? ?sw.Start();
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ?SqlBulkCopy sbc = new SqlBulkCopy(config, SqlBulkCopyOptions.UseInternalTransaction);
? ? ? ? ? ? ? ? ? ?sbc.BulkCopyTimeout = 500;//超時時間默認30秒鐘
? ? ? ? ? ? ? ? ? ?sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnRowsCopied); //處理完成指定行數后觸發的事件;這里不是必須的;
? ? ? ? ? ? ? ? ? ?sbc.NotifyAfter = dt.Rows.Count; //生成通知事件前要處理的行數
? ? ? ? ? ? ? ? ? ?sbc.DestinationTableName = "dbo.ss"; //對應數據庫的表名
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?sw.Stop();
? ? ? ? ? ? ? ?double dd = sw.Elapsed.TotalSeconds;
? ? ? ? ? ?}
? ? ? ? ? ?catch (Exception ex)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?throw ex;
? ? ? ? ? ?}
? ? ? ?}
? ? //這里實際是可以不用的,但是為了說明用法,我就寫了個方法
? ? ? ?private static void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
? ? ? ?{
? ? ? ? ? ?string ss = "";
? ? ? ? ? ?ss += args.RowsCopied.ToString() + " rows are copied<Br>";
? ? ? ?}
? ?}
}
-- 下面是逐次向表中插入數據的的用時前面表示的條數后面是秒數;注釋中的數據表示表中已有數據
select 1000000/16 ?-- 0 ?1個字段
select 1000000/7.90 -- 一百萬 ?一個字段
select 1000000/8.97 -- 二百萬 ?一個字段
select 1000000/7.60 -- 三百萬 ?一個字段
select 1000000/6.54 -- 4百萬 ? 一個字段
select 1000000/10.3626133 ?-- 五百萬 ?兩個字段
select 1000000/8.7146654 ?-- 六百萬 ?兩個字段
select 1000000/10.2771326 ?-- 八百萬 ?兩個字段
select 1000000/10.2771326 ?-- 九百萬 ? 兩個字段
select 1000000/7.4966066 ?-- 一千萬 ?兩個字段
需要注意下面的事項:
如果在創建表的時候如下面所示,id是自增列有默認值得話,插入式正常的
create(name varchar(50),sex varchar(20),id int identity(1,1)not null);
create(id int identity(1,1)not null,name varchar(50),sex varchar(20));
轉載于:https://blog.51cto.com/tianle52/1392474
總結
以上是生活随笔為你收集整理的大数据批量插入小练习_SqlServer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 Windows Azure 上部署并
- 下一篇: Cisco路由器全局、接口、协议调试指南