Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbContext ChangeTracking kills performance?

I am in the process of upgrading an application from EF1 to EF4.1 I created a DbContext and a set of POCOs using the "ADO.NET DbContext Generator" templates.

When I query the generated DbContext the database part of the query takes 4ms to execute (validated with EF Profiler). And then it takes the context about 40 seconds (in words: FORTY!) to do whatever it does before it returns the result to the application.

EF1 handles the same query in less than 2 seconds.

Turning off AutoDetectChanges, LazyLoading and ProxyGeneration wins me 2-3 seconds.

When I use the AsNoTracking() extension method I am able to reduce the total execution time to about 3 seconds.

That indicates that ChangeTracking is the culprit.

But ChangeTracking is what I need. I must be able to eventually persist all changes without having to handpick which entities were modified.

Any ideas how I could solve that performance issue?

like image 981
Sebastian Weber Avatar asked May 12 '11 07:05

Sebastian Weber


1 Answers

Is the technique at the end of this documentation useful? Alternatively, I've avoided many of the performance pitfalls using a fluent interface to declaratively state which entities in a given transaction for sure won't change vs. might change (immutable vs. immutable). For example, if the entities I am saving are aggregate roots in which the root or its entities refer to "refdata" items, then this heuristic prevents many writes because the immutable items don't need to be tracked. The mutable items all get written without check (a weakness... One which may or may not be acceptable).

I'm using this with a generic repository pattern precisely because I don't want to track changes or implement a specific strategy for each case. If that's not enough, perhaps rolling your own change tracking outside of the context and adding entities in as needed will work.

like image 116
Kit Avatar answered Oct 05 '22 15:10

Kit