-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a large number of entities to the DbContext is slow #4831
Comments
Workaround could be to use two separate DbContexts, one where navigations would only be exposed as FKs, that would be used to persist the data, and then another, proper DbContext to do all the querying and other processing. |
Did you try to disable AutoDetectChangesEnabled?
It always helped me do a batch inserts in previous EF's (it was in |
Looks like this StackOverflow question is seeing similar results. |
@cbmek I did some more investigation on this and long story short, AutoDetectChangesEnable = false doesn't make things much better. However breaking stuff into separate SaveChanges calls using separate contexts to do so improves the performance drastically. I have the following entities: I saved 10 batches and took the average time it took. Conducted the following scenarios:
|
This would be expected since we don't run this logic during each |
Issue #4831 The problem was in the code that propagates FK values from principal values when adding dependents. It was doing expensive scanning with a TODO: Perf comment. :-) The fix is to reuse the look-aside indexes already created for fixup. For large graphs the perf is now orders of magnitude faster.
Issue #4831 The problem was in the code that propagates FK values from principal values when adding dependents. It was doing expensive scanning with a TODO: Perf comment. :-) The fix is to reuse the look-aside indexes already created for fixup. For large graphs the perf is now orders of magnitude faster.
In my scenario I'm adding about 5000 entities, each has about 2-7 entities via 1-Many relationship (there is about 18000 of those "leaf" entities). If done all at once persisting it all takes 3 min 4 sec. If done in chunks of about 500-1000 it takes 47 sec. Large portion of time is spent on adding those parent entities to the context (and I guess walking the graph and fixing up the relationships).
For my scenario however this is not necessary - I only wish to persist those entities, I generate all the keys and fixup the relationships manually via foreign keys. Perhaps we should consider a way to disable all the change tracking, for scenarios similar to this?
The text was updated successfully, but these errors were encountered: