Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Added AsNoTracking for readonly queries #136

Closed
wants to merge 1 commit into from

Conversation

neyromant
Copy link

No description provided.

Copy link

@LindaLawton LindaLawton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neyromant
Copy link
Author

neyromant commented Feb 14, 2018

@LindaLawton Yes, It avoids the overhead of setting up change tracking for each entity instance. See https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.asnotracking?view=efcore-2.0

@scottbrady91
Copy link
Member

This is fine, assuming no one is using these classes and relying on this behavior (which would be weird).

Do you think there will still be a performance improvement given that the dbcontexts are registered as scoped and some of these methods are hit multiple times? I'm wondering if there's a perf decrease for developers not using the store caching.

@neyromant
Copy link
Author

@scottbrady91 Note from https://docs.microsoft.com/en-us/ef/core/querying/tracking:

No tracking queries still perform identity resolution within the excuting query. If the result set contains the same entity multiple times, the same instance of the entity class will be returned for each occurrence in the result set. However, weak references are used to keep track of entities that have already been returned. If a previous result with the same identity goes out of scope, and garbage collection runs, you may get a new entity instance. For more information, see How Query Works.

@LindaLawton
Copy link

@neyromant very smart i hadnt seen this before. I will be adding it to a few projects.

@neyromant
Copy link
Author

I have prepared some simple benchmarks for AsNoTracking (https://github.com/neyromant/Benchmarks/blob/master/Benchmarks/AsNoTracking/AsNoTrackingTests.cs) using BenchmarkDotNet (https://github.com/dotnet/BenchmarkDotNet)

Here is the results:

Method Records DbType Mean Error StdDev Scaled Gen 0 Gen 1 Gen 2 Allocated
Track 1 InMemory 1,997.5 us 13.308 us 12.448 us 1.00 42.9688 23.4375 7.8125 166.06 KB
NoTrack 1 InMemory 1,766.5 us 10.005 us 8.355 us 0.88 42.9688 21.4844 9.7656 164.92 KB
Track 1 SqlLight 962.1 us 4.921 us 4.362 us 1.00 3.9063 - - 19.45 KB
NoTrack 1 SqlLight 950.4 us 1.304 us 1.156 us 0.99 3.9063 - - 18.95 KB
Track 10 InMemory 1,991.6 us 4.890 us 4.574 us 1.00 44.9219 21.4844 9.7656 169.07 KB
NoTrack 10 InMemory 1,835.1 us 3.521 us 3.294 us 0.92 42.9688 21.4844 9.7656 165.53 KB
Track 10 SqlLight 1,011.0 us 2.834 us 2.512 us 1.00 5.8594 - - 24.33 KB
NoTrack 10 SqlLight 968.5 us 4.634 us 3.870 us 0.96 3.9063 - - 20.74 KB
Track 100 InMemory 2,195.0 us 7.693 us 7.196 us 1.00 50.7813 27.3438 7.8125 202.41 KB
NoTrack 100 InMemory 1,884.8 us 7.144 us 6.333 us 0.86 42.9688 19.5313 9.7656 166.59 KB
Track 100 SqlLight 1,423.6 us 8.378 us 7.837 us 1.00 17.5781 - - 74.24 KB
NoTrack 100 SqlLight 1,091.7 us 5.925 us 5.542 us 0.77 7.8125 - - 38.19 KB
Track 1000 InMemory 4,997.9 us 30.450 us 28.483 us 1.00 101.5625 39.0625 15.6250 534.86 KB
NoTrack 1000 InMemory 1,957.9 us 3.175 us 2.652 us 0.39 42.9688 23.4375 7.8125 173.64 KB
Track 1000 SqlLight 5,880.8 us 43.999 us 41.156 us 1.00 117.1875 39.0625 - 575.44 KB
NoTrack 1000 SqlLight 2,417.2 us 11.036 us 10.323 us 0.41 50.7813 11.7188 - 214.11 KB

@brockallen
Copy link
Member

Since we reorganized our repos, I did this change manually. We're tracking here for the release notes: IdentityServer/IdentityServer4#2518.

Thanks!

@brockallen brockallen closed this Aug 4, 2018
@neyromant
Copy link
Author

So, I will not be on the list of contributors? Well, all right.

@brockallen
Copy link
Member

Sorry -- if we hadn't reorganized the repos, then you would have.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants