代码之家  ›  专栏  ›  技术社区  ›  hkjhadj1

同时添加数百万条记录[关闭]

  •  -2
  • hkjhadj1  · 技术社区  · 7 年前

    我有一个asp.NETMVC5应用程序,我用它来存储数据。在10个不同的excel文件中总共有8500万条记录。我上传一个excel文件,系统将文件中的记录存储到我的数据库中。不过,这需要很多时间,65K条记录大约需要1个小时左右。有什么办法可以加快速度吗?这些速度正常吗?

    我正在使用mvc5和mssql。从文件读取并写入excel的代码为:

                    //Reads excel file using ExcelDataReader Package
                    var dataTable = result.Tables[0];
    
                    //Read each row one by one
                    for (var i = 0; i < dataTable.Rows.Count; i++)
                    {
                        //Read Properties
                        var FName = dataTable.Rows[i][0].ToString().Trim(); //First Name
                        //This goes on, I have 11 properties
    
                        //Create DbEntity
                        var dbEntity = new DbEntity
                        {
                            FirstName = FName,
                            //Do the same for all other properties
                        };
    
                        var entities = db.DbEntities.Where(d => d.Phone == dbEntity.Phone).ToList();
    
                        if (entities.Count() > 0) 
                        {
                            //If it is duplicate, set IsDuplicate to true
                            dbEntity.IsDuplicate = true;
    
                            //Set occurance = count(entities) + 1
                            dbEntity.Ocurance = entities.Count() + 1;
                        }
                        else
                        {
                            //If the entity is unique, set IsDuplicate to false
                            dbEntity.IsDuplicate = false;
    
                            //Set the occurance to 1
                            dbEntity.Ocurance = 1;
                        }
    
                        //Set WasSent to false
                        dbEntity.WasSent = false;
    
                        //Add Entity to records
                        if(dbEntity.Phone.Length == 10)
                        {
                            db.DbEntities.Add(dbEntity);
                            db.SaveChanges();
                            ++validCount;
                        }
                        else
                        {
                            //If record is not valid, skip it and add it to invalidRec list
                            invalidRecs.Add(dbEntity);
                        }
                    }
    
                    reader.Close();
                    //Sending result data to View
    
                    var data = new ImportResultViewModel
                    {
                        ValidCount = validCount,
                        InvalidList = invalidRecs
                    };
                    return View("ImportResult",data);
                }
    

    如您所见,我将每条记录逐一添加。如果我将所有有效记录保存在一个列表中,然后将整个列表添加到数据库中,会更好吗?这会提高性能吗?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Kevin B Burns    7 年前

    https://libraries.io/nuget/SqlBulkTools

    我对在生产中使用它很有信心,但每个人的参数都不一样。如果您真的想提高性能,并且更喜欢ADO.Net(原始),仍然有两个选项可用:

    (批量复制) https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/single-bulk-copy-operations

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/performing-batch-operations-using-dataadapters