加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用.NET更新(填充)1,000,000条记录到数据库中的最快方法

发布时间:2020-12-15 06:18:35 所属栏目:百科 来源:网络整理
导读:我正在使用此代码将100万条记录插入到数据库的空表中.好的,所以没有多少代码,我将从我已经与数据交互的角度开始,并将模式读入DataTable: 所以: DataTable returnedDtViaLocalDbV11 = DtSqlLocalDb.GetDtViaConName(strConnName,queryStr,strReturnedDtName
我正在使用此代码将100万条记录插入到数据库的空表中.好的,所以没有多少代码,我将从我已经与数据交互的角度开始,并将模式读入DataTable:

所以:

DataTable returnedDtViaLocalDbV11 = DtSqlLocalDb.GetDtViaConName(strConnName,queryStr,strReturnedDtName);

现在我们已经返回了DtViaLocalDbV11,让我们创建一个新的DataTable作为源数据库表的克隆:

DataTable NewDtForBlkInsert = returnedDtViaLocalDbV11.Clone();

Stopwatch SwSqlMdfLocalDb11 = Stopwatch.StartNew();
NewDtForBlkInsert.BeginLoadData();

for (int i = 0; i < 1000000; i++)
{
   NewDtForBlkInsert.LoadDataRow(new object[] { null,"NewShipperCompanyName"+i.ToString(),"NewShipperPhone" },false);
}
NewDtForBlkInsert.EndLoadData();

DBRCL_SET.UpdateDBWithNewDtUsingSQLBulkCopy(NewDtForBlkInsert,tblClients._TblName,strConnName);

SwSqlMdfLocalDb11.Stop();

var ResSqlMdfLocalDbv11_0 = SwSqlMdfLocalDb11.ElapsedMilliseconds;

此代码在5200ms内将100万条记录填充到嵌入式SQL数据库(localDb).其余的代码只是实现bulkCopy,但我会发布.

public string UpdateDBWithNewDtUsingSQLBulkCopy(DataTable TheLocalDtToPush,string TheOnlineSQLTableName,string WebConfigConName)
 {
    //Open a connection to the database. 
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[WebConfigConName].ConnectionString))
    {
       connection.Open();

       // Perform an initial count on the destination table.
       SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM "+TheOnlineSQLTableName +";",connection);
       long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());

       var nl = "rn";
       string retStrReport = "";
       retStrReport = string.Concat(string.Format("Starting row count = {0}",countStart),nl);
       retStrReport += string.Concat("==================================================",nl);
       // Create a table with some rows. 
       //DataTable newCustomers = TheLocalDtToPush;

       // Create the SqlBulkCopy object.  
       // Note that the column positions in the source DataTable  
       // match the column positions in the destination table so  
       // there is no need to map columns.  
       using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
       {
          bulkCopy.DestinationTableName = TheOnlineSQLTableName;

          try
          {
             // Write from the source to the destination.
             for (int colIndex = 0; colIndex < TheLocalDtToPush.Columns.Count; colIndex++)
             {
                bulkCopy.ColumnMappings.Add(colIndex,colIndex);
             }
             bulkCopy.WriteToServer(TheLocalDtToPush);
          }

          catch (Exception ex)
          {
             Console.WriteLine(ex.Message);
          }
       }

       // Perform a final count on the destination  
       // table to see how many rows were added. 
       long countEnd = System.Convert.ToInt32(
       commandRowCount.ExecuteScalar());

       retStrReport += string.Concat("Ending row count = ",countEnd,nl);
       retStrReport += string.Concat((countEnd - countStart)," rows were added.",nl);
       retStrReport += string.Concat("New Customers Was updated successfully",nl,"END OF PROCESS !");
       //Console.ReadLine();
       return retStrReport;
   }
}

通过连接到SQL服务器尝试它是大约7000ms(最多)& ?7700ms平均.另外通过一个随机的kv nosql数据库花费大约40秒(真的,我甚至没有保留它的记录,因为它通过x2的sql变体).那么…有比我在代码中测试的更快的方法吗?

编辑

我正在使用win7 x64 8gb ram,最重要的是我应该认为(像i5 3ghz)现在不是很好
Raid-0上的x3 500Gb Wd的工作更好
但我只是说如果你会检查你的电脑
虽然只是将其与您的配置中的任何其他方法进行比较

解决方法

你试过SSIS吗我从来没有写过一个带有loacldb连接的SSIS包,但这是SSIS应该非常适合的活动.

如果您的数据源是SQL Server,另一个想法将是设置链接的服务器.不知道这是否适用于localdb.如果可以设置链接的服务器,则可以绕过C#,并使用INSERT .. SELECT … FROM … SQL语句加载数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读