[英]How to optimise inserting multiple records (with exists check) via Entity Framework
I have a folder filled with about 200 csv files, each containing about 6000 rows of data containing mutual fund data.我有一个文件夹,里面有大约 200 个 csv 文件,每个文件包含大约 6000 行包含共同基金数据的数据。 I have to copy those comma separated data into the database via Entity Framework.我必须通过实体框架将这些逗号分隔的数据复制到数据库中。
The two major objects are Mutual_Fund_Scheme_Details
and Mutual_Fund_NAV_Details
.两个主要对象是Mutual_Fund_Scheme_Details
和Mutual_Fund_NAV_Details
。
Mutual_Fund_Scheme_Details
- this contains columns like Scheme_Name, Scheme_Code, Id, Last_Updated_On. Mutual_Fund_Scheme_Details
- 这包含 Scheme_Name、Scheme_Code、Id、Last_Updated_On 等列。
Mutual_Fund_NAV_Details
- this contains Scheme_Id (foreign key), NAV, NAV_Date. Mutual_Fund_NAV_Details
- 这包含 Scheme_Id(外键)、NAV、NAV_Date。
Each line in the CSV contains all of the above columns so before inserting, I have to - CSV 中的每一行都包含上述所有列,因此在插入之前,我必须 -
All the exists checks are done using ANY linq extension method and all the new entries are inserted into the DbContext
but the SaveChanges
method is called only at the end of processing of each file.所有存在检查都是使用 ANY linq 扩展方法完成的,所有新条目都插入到DbContext
,但SaveChanges
方法仅在每个文件处理结束时调用。 I used to call it after each insert but that just takes even longer than right now.我曾经在每次插入后调用它,但这只需要比现在更长的时间。
Now since, this involves at least two exists checks, at the most two inserts and one update, the insertion of each file is taking too long close to 5-7 minutes per file.现在,由于这涉及至少两个存在检查,最多两个插入和一个更新,每个文件的插入花费的时间太长,每个文件接近 5-7 分钟。 I am looking for suggestions to improve this.我正在寻找改进这一点的建议。 Any help would be useful.任何帮助都会很有用。
Specifically, I am looking to:具体来说,我希望:
It's going to be hard to optimize it with EF.使用 EF 将很难对其进行优化。 Here is a suggestion:这是一个建议:
.Where( x => listOfIdsFromFile.Contains(x.Id))
.处理整个文件(~6000)后,使用.Where( x => listOfIdsFromFile.Contains(x.Id))
进行存在检查。 This should work for 6000 ids and it will allow you separate inserts from updates.这应该适用于 6000 个 id,它将允许您将插入与更新分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.