Repository navigation
Open
Milestone
Description
Include or Where .AsNotracking queries seems to perform worse in EF Core than leaving the .AsNotracking property out. This was not the case in EF 6.
So while DbSet.AsNoTracking.ToList() is slightly better or on par with DbSet.AsNoTracking.ToList()
then
DbSet.Where.AsNoTracking.ToList()
DbSet.Include.AsNoTracking.ToList()
DbSet.Include.Where.AsNoTracking.ToList()performance a lot worse than
DbSet.Where.ToList()
DbSet.Include.ToList()
DbSet.Include.Where.ToList()respectively.
Steps to reproduce
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0} Start EF Core", DateTime.Now);
using (var context = new BlogContext())
{
context.ChangeTracker.AutoDetectChangesEnabled = false;
// Create new database
//Console.WriteLine("{0} Create Database", DateTime.Now);
//context.Database.EnsureDeleted();
//context.Database.EnsureCreated();
// Add blog posts
//Console.WriteLine("{0} Add Blog Posts", DateTime.Now);
//for (int i = 0; i < 100000; i++)
//{
// var blog = new Blog();
// blog.Posts = new List<Post>();
// for (int j = 0; j < 10; j++)
// {
// var post = new Post();
// blog.Posts.Add(post);
// }
// context.Blogs.Add(blog);
// context.SaveChanges();
//}
// Query blogs
List<Post> posts;
Console.WriteLine("{0} Query blog posts", DateTime.Now);
posts = context.Posts.ToList();
Console.WriteLine("{0} context.Posts.ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.ToList();
Console.WriteLine("{0} context.Posts.ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.AsNoTracking().ToList();
Console.WriteLine("{0} context.Posts.AsNoTracking().ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Where(p => p.Blog.BlogID > 100).ToList();
Console.WriteLine("{0} context.Posts.Where(p => p.Blog.BlogID > 100).ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Where(p => p.Blog.BlogID > 100).AsNoTracking().ToList();
Console.WriteLine("{0} context.Posts.Where(p => p.Blog.BlogID > 100).AsNoTracking().ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Include(p => p.Blog).ToList();
Console.WriteLine("{0} context.Posts.Include(p => p.Blog).ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Include(p => p.Blog).AsNoTracking().ToList();
Console.WriteLine("{0} context.Posts.Include(p => p.Blog).AsNoTracking().ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Include(p => p.Blog).Where(p => p.Blog.BlogID > 100).ToList();
Console.WriteLine("{0} context.Posts.Include(p => p.Blog).Where(p => p.Blog.BlogID > 100).ToList() {1}", DateTime.Now, posts.Count);
posts = context.Posts.Include(p => p.Blog).Where(p => p.Blog.BlogID > 100).AsNoTracking().ToList();
Console.WriteLine("{0} context.Posts.Include(p => p.Blog).Where(p => p.Blog.BlogID > 100).AsNoTracking().ToList() {1}", DateTime.Now, posts.Count);
}
Console.WriteLine("{0} End", DateTime.Now);
}
}
public class Blog
{
public int BlogID { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostID { get; set; }
public int BlogID { get; set; }
public Blog Blog { get; set; }
}
public class BlogContext: DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=EFCore;Trusted_Connection=True;");
}
}
}Reacted by mohammed-fuad, boukenka, tale80, Meryovi De Dios, Tim, parys, cyril265, Bart Koelman, Christiaan Rakowski, the-unrepentant-nerd and 1 more
Activity
ajcvickers commentedon Jan 10, 2019
@HjalteParner There are a couple dimensions beyond tracking/no-tracking here, so I updated your test code to separate them out more--see below. The dimensions I split out are:
Here's what I see:
Observations:
We'll look at this in triage, but from what I see the results look basically expected.
HjalteParner commentedon Jan 11, 2019
@ajcvickers Thanks for your comprehensive answer and your refactoring of my test code, which is much neater now.
You are correct that my issue has more to do with the Include and Where clause than Tracking vs. No Tracking and in addition even more to do with the performance comparison between EF Core vs. EF6 so the title should probably be changed to reflect this, if you agree that there is an issue with EF Core.
Unfortunately having already tracked entities is not possible for me as my production database is huge compared to the test database presented here - which is why I need the Where clause
in addition to the some details from the parent (in fact in the production, it's from the grandparent) as well - which is why i need the include (join).
I have made a test using your refactored code on my computer using both EF6 and EF Core.
Below is my results:
EF6
Running once to prime the query cache...
Test case ToList ran 1 iterations in 30635ms
Test case NoTrackingToList ran 1 iterations in 2329ms
Test case Where ran 1 iterations in 27338ms
Test case NoTrackingWhere ran 1 iterations in 1649ms
Test case Include ran 1 iterations in 40649ms
Test case NoTrackingInclude ran 1 iterations in 15693ms
Test case WhereInclude ran 1 iterations in 40526ms
Test case NoTrackingWhereInclude ran 1 iterations in 15316ms
Running with posts already tracked...
Test case ToList ran 1 iterations in 1208ms
Test case NoTrackingToList ran 1 iterations in 1852ms
Test case Where ran 1 iterations in 1363ms
Test case NoTrackingWhere ran 1 iterations in 1835ms
Test case Include ran 1 iterations in 21641ms
Test case NoTrackingInclude ran 1 iterations in 17399ms
Test case WhereInclude ran 1 iterations in 22569ms
Test case NoTrackingWhereInclude ran 1 iterations in 18567ms
Running with posts not tracked...
Test case ToList ran 1 iterations in 30367ms
Test case NoTrackingToList ran 1 iterations in 2556ms
Test case Where ran 1 iterations in 25095ms
Test case NoTrackingWhere ran 1 iterations in 2278ms
Test case Include ran 1 iterations in 38989ms
Test case NoTrackingInclude ran 1 iterations in 15146ms
Test case WhereInclude ran 1 iterations in 36872ms
Test case NoTrackingWhereInclude ran 1 iterations in 15044ms
EF Core
Running once to prime the query cache...
Test case ToList ran 1 iterations in 9613ms
Test case NoTrackingToList ran 1 iterations in 1278ms
Test case Where ran 1 iterations in 76336ms
Test case NoTrackingWhere ran 1 iterations in 58301ms
Test case Include ran 1 iterations in 123529ms
Test case NoTrackingInclude ran 1 iterations in 82854ms
Test case WhereInclude ran 1 iterations in 121332ms
Test case NoTrackingWhereInclude ran 1 iterations in 70365ms
Running with posts already tracked...
Test case ToList ran 1 iterations in 1623ms
Test case NoTrackingToList ran 1 iterations in 1132ms
Test case Where ran 1 iterations in 1610ms
Test case NoTrackingWhere ran 1 iterations in 58405ms
Test case Include ran 1 iterations in 16745ms
Test case NoTrackingInclude ran 1 iterations in 78411ms
Test case WhereInclude ran 1 iterations in 20362ms
Test case NoTrackingWhereInclude ran 1 iterations in 74163ms
Running with posts not tracked...
Test case ToList ran 1 iterations in 9239ms
Test case NoTrackingToList ran 1 iterations in 1098ms
Test case Where ran 1 iterations in 72227ms
Test case NoTrackingWhere ran 1 iterations in 57913ms
Test case Include ran 1 iterations in 116401ms
Test case NoTrackingInclude ran 1 iterations in 68680ms
Test case WhereInclude ran 1 iterations in 120856ms
Test case NoTrackingWhereInclude ran 1 iterations in 71566ms
From above I gather:
I would expect WhereInclude to perform somewhere in between e.g. closer to basic as Where performs better and Include performs worse. However this is not quite the case for either framework but again EF Core is much slower than EF6 - slower by a factor 12/70 vs 1.2/14 respectively.
Last but not least!
ajcvickers commentedon Jan 11, 2019
@HjalteParner Thanks for the additional information. This does indeed look worrying; we will discuss. /cc @divega
ajcvickers commentedon Jan 14, 2019
Triage: We need to investigate this further for 3.0. However, this should happen after the major query changes are merged.
peterkfa commentedon Apr 10, 2019
I am also experiencing an increase of anywhere from 20%-200% in execution time when adding .AsNoTracking() to my query when using EFCore 2.2.4. While my query is much more complex than the one above, it does include multiple joins, includes, and where clauses.
This was tested using BenchmarkDotNet with warmup enabled and 20 iterations of the test run per method against a SQL database without any possible external factors affecting it. I ran the test in various configurations and got the same consistent result of AsNoTracking slowing things down appreciably.
|----------------------Method-|-----Mean-|----Error-|---StdDev-|------Min-|------Max-||-----------------------------|---------:|---------:|---------:|---------:|---------:||--------GetWithTrackingAsync-|--68.11-s-|-0.9863-s-|-0.6524-s-|--67.11-s-|--69.11-s-||------GetWithNoTrackingAsync-|-173.83-s-|-1.7734-s-|-1.1730-s-|-172.19-s-|-175.58-s-|2 remaining items
tap-absg commentedon Jul 3, 2019
Looking at this, I modified @ajcvickers code to use the In Memory database and DI so I could use the Db Context Pooling and remove localdb from the equation. My results are here
There probably should be a warning added to https://docs.microsoft.com/en-us/ef/core/querying/tracking about this defect
pgrm commentedon Jan 19, 2020
@ajcvickers - for those of us who might have found this question and are concerned enabling AsNoTracking for all queries which don't need tracking, by default - is this only an issue in specific cases? Does it still exist in ef core 3.1.1?
thx for the insights
ajcvickers commentedon Jan 22, 2020
@pgrm We haven't root-caused the issue yet. It's certainly only an issue in specific cases. If you're concerned about perf here, then I think you'll need to do perf testing on your application.
mkanakis commentedon Jan 24, 2020
@pgrm
For what is worth, in my case for two different test scenarios average query duration between tracking and no-tracking is:
It seems that no-tracking is doing a better job, even though the numbers are not significant in my opinion.
It also seems, that as @ajcvickers mentioned it is case-specific and not general.
midgleyc commentedon Aug 2, 2022
I've just hit a query (in 6.0.7) that performs significantly worse (~5x slower) under
AsNoTrackingcompared to with tracking. It callsIncludeusing a component mapped to a view.By changing
AsNoTrackingintoAsNoTrackingWithIdentityResolutionthe speed penalty goes away.Mateusz19021996 commentedon Jan 18, 2023
Any updates on it?
ajcvickers commentedon Jan 18, 2023
This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 8.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.