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

标签 c# .net sql nosql localdb

我正在使用这段代码将 100 万条记录插入到数据库的一个空表中。好的,不用太多代码,我将从我已经与数据交互的那一点开始,并将模式读入 DataTable:

所以:

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

现在我们有 returnedDtViaLocalDbV11 让我们创建一个新的 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;

此代码在 5200 毫秒 内将 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 = "\r\n";
       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("==================================================", 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 服务器的连接进行尝试大约需要 7000 毫秒(最好),平均大约需要 7700 毫秒。同样通过一个随机的 kv nosql 数据库花费了大约 40 秒(实际上我什至没有保留它的记录,因为它传递了 x2 的 sql 变体)。那么...有没有比我在代码中测试的方法更快的方法?

编辑

我正在使用 win7 x64 8gb 内存,最重要的是我认为(因为 i5 3ghz)现在还不是很好 Raid-0 上的 x3 500Gb Wd 做得更好 但我只是说你是否会检查一下你的电脑 虽然只是将它与您配置中的任何其他方法进行比较

最佳答案

您尝试过 SSIS 吗?我从来没有写过带有 loacldb 连接的 SSIS 包,但这是 SSIS 应该非常适合的那种事件。

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

关于c# - 使用 .NET 将 1,000,000 条记录更新(填充)到数据库中的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17281412/

相关文章:

c# - iTextSharp - 如何将 PDFPRow 添加到 PDFPTable?

c# - 强制下载文件,默认不在浏览器上打开它

php - 如何在 Eloquent 中使用 DATEDIFF 函数?

c# - c#查询结果中记录计数

c# - 在 winforms 应用程序上绘制相反(反向)颜色

c# - 如何绑定(bind)到通用 Windows 应用程序模板内的空值?

c# - 打开 XML 文件时出错(错误名称异常)

.net - 反射还是动态方法?

c# - 参数匹配不能与 NSubstitute 一起正常工作

sql - "select ... where in"相等并加入