In the first place, I am very aware that there are many other articles out there sharing the exact topic. However, those articles are old. For example,
Entity Framework is Too Slow. What are my options? this one is 4 years ago, and I assume the framework has released at least one major version update so I hope something new and helpful can help me out on this topic.
So my problem in detail is like this:
I have a data source that I have to parse and insert the results into the sql server. Since the parsing is so fast, I think the overhead is obviously related to entity framework.
I need to insert data into a few dozens of tables, around 80 of them. Each table has 8 columns, say, at average, among which there is an auto-incremental id column. And each time, I properly need to insert a few thousands rows into say, 20 tables. therefore some table are huge(maybe 100k rows?)
The tables have relation between them, which is simply a star form relation, like this:
table5 table6 \ / table3-----------\ \ / /-----------------table4 table1 ------------ record table ------------------ table2
just imagine the main table has 80 tables around it. 90% of the relation is 1-to-many, some many-to-many, limited one-to-one.
The way I do insertion is, I add all the result into context by using AddRange
method, and I call SaveChanges
once globally.
I observed my insertion efficiency in current settings, and I found at the very beginning, the insertion takes a few seconds, then minutes, now it takes 30 minutes to insert data roughly in the same size.
So my question is that is there any good way to boost the insertion speed up? I am aware of bulk insert, but I have inheritance relation so I am seeking any other way that is not going to break the relation constraint.
Would it help in this situation if I define stored procedure to serve the insertion purpose? I am sorry but actually I just learned it for weeks and expecting it is a right tool to use for business logic.