question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Large performance difference with EF6 join vs EF7 include

See original GitHub issue

Windows 10, VS 2015, IIS Express, SQL Server Express 2014, EF 7.0.0-beta7, ASP.NET 6.0.0-beta7

I am seeing a large performance difference for a simple join in EF7 beta7 vs EF6. Using these models:

public class Blog
{
        public int BlogId { get; set; }

        [StringLength(100)]
        public String Name { get; set; }

        public DateTime DateStarted { get; set; }

        public String AuthorName { get; set; }

        public ICollection<Post> Posts { get; set; }
}
 public class Post
 {
        public int PostId { get; set; }

        public int BlogId { get; set; }

        public Blog Blog { get; set; }

        public DateTime DatePosted { get; set; }

        public String PostHtml { get; set; }
  }

Join code for EF6:

session.All<Post>().Join(session.All<Blog>(), b => b.BlogId, p => p.BlogId,
                (post, blog) => new TestModel {
                    Post = post,
                    Blog = blog
                }).ToList();

Which generates SQL:

SELECT 
[Extent1].[PostId] AS [PostId], 
[Extent1].[BlogId] AS [BlogId], 
[Extent1].[DatePosted] AS [DatePosted], 
[Extent1].[PostHtml] AS [PostHtml], 
[Extent2].[BlogId] AS [BlogId1], 
[Extent2].[AuthorName] AS [AuthorName], 
[Extent2].[DateStarted] AS [DateStarted], 
[Extent2].[Name] AS [Name]
FROM  [dbo].[Post] AS [Extent1]
INNER JOIN [dbo].[Blog] AS [Extent2] ON [Extent1].[BlogId] = [Extent2].[BlogId]

vs. EF7 join code:

_dbContext.Posts.Include(b => b.Blog).ToList();

Which generates SQL:

SELECT [b].[PostId], [b].[BlogId], [b].[DatePosted], [b].[PostHtml], [b0].[BlogId], [b0].[AuthorName], [b0].[DateStarted], [b0].[Name]
FROM [Post] AS [b]
INNER JOIN [Blog] AS [b0] ON [b].[BlogId] = [b0].[BlogId]

Both have similar execution plans, and with test data of 100 blogs and 100 posts per blog, run in a fraction of a second from SQL Server Management Studio. The EF6 query also runs in a fraction of a second. However, the EF7 query takes several seconds and causes IIS to garbage collect a number of times while it is running.

Here is the SQL Server Profiler output. The EF6 query is first, followed by the EF7 one:

capture

Obviously this example is contrived, joining from many to few, but the actual code we have requires a similar join - starting from a cross-reference table and .Includeing to multiple source tables, where we are also seeing this performance issue.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ahmad-moussawicommented, Dec 24, 2015

(y) I think Important note like this, should be mentioned in the docs

0reactions
jparish9commented, Oct 9, 2015

👍 This is for a web API so most of the queries returning a large result set are read-only. Thanks for the heads-up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Large performance difference with EF6 join vs EF7 include
Both have similar execution plans, and with test data of 100 blogs and 100 posts per blog, run in a fraction of a...
Read more >
EntityFramework include vs join performance
I'd like to know, when using entity framework, which one yields better performance? I've read that if you have foreign relationships between ...
Read more >
Entity Framework Core 7 (EF7) is available today - .NET Blog
EF Core 7 is the successor to EF Core 6, and can be referred to as EF7 for brevity. EF Core 7 contains...
Read more >
Efficient Querying - EF Core
Performance guide for efficient querying using Entity Framework Core.
Read more >
Entity Framework Core and high performance
For EF7, we plan to focus on performance related to database inserts and updates. This includes performance of change-tracking queries, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found