Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:02:49+00:00 2026-05-25T13:02:49+00:00

I am running into an interesting performance issue with Entity Framework. I am using

  • 0

I am running into an interesting performance issue with Entity Framework. I am using Code First.

Here is the structure of my entities:

A Book can have many Reviews.
A Review is associated with a single Book.
A Review can have one or many Comments.
A Comment is associated with one Review.

public class Book
{
    public int BookId { get; set; }
    // ...
    public ICollection<Review> Reviews { get; set; }
}

public class Review 
{
    public int ReviewId { get; set; }
    public int BookId { get; set; }
    public Book Book { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

public class Comment
{
     public int CommentId { get; set; }
     public int ReviewId { get; set; }
     public Review Review { get; set; }
}

I populated my database with a lot of data and added the proper indexes. I am trying to retrieve a single book that has 10,000 reviews on it using this query:

var bookAndReviews = db.Books.Where(b => b.BookId == id)
                       .Include(b => b.Reviews)
                       .FirstOrDefault();

This particular book has 10,000 reviews. The performance of this query is around 4 seconds. Running the exact same query (via SQL Profiler) actually returns in no time at all. I used the same query and a SqlDataAdapter and custom objects to retrieve the data and it happens in under 500 milliseconds.

Using ANTS Performance Profiler it looks like a bulk of the time is being spent doing a few different things:

The Equals method is being called 50 million times.

Does anyone know why it would need to call this 50 million times and how I could increase the performance for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:02:50+00:00Added an answer on May 25, 2026 at 1:02 pm

    Why is Equals called 50M times?

    It sounds quite suspicious. You have 10.000 reviews and 50.000.000 calls to Equals. Suppose that this is caused by identity map internally implemented by EF. Identity map ensures that each entity with unique key is tracked by the context only once so if context already has instance with the same key as loaded record from the database it will not materialize new instance and instead uses the existing one. Now how this can coincide with those numbers? My terrifying guess:

    =============================================
    1st      record read   |  0     comparisons
    2nd      record read   |  1     comparison
    3rd      record read   |  2     comparisons
    ...
    10.000th record read   |  9.999 comparisons
    

    That means that each new record is compared with every existing record in identity map. By applying math to compute sum of all comparison we can use something called “Arithmetic sequence”:

    a(n) = a(n-1) + 1
    Sum(n) = (n / 2) * (a(1) + a(n))
    Sum(10.000) = 5.000 * (0 + 9.999) => 5.000 * 10.000 = 50.000.000
    

    I hope I didn’t make mistake in my assumptions or calculation. Wait! I hope I did mistake because this doesn’t seem good.

    Try turning off change tracking = hopefully turning off identity map checking.

    It can be tricky. Start with:

    var bookAndReviews = db.Books.Where(b => b.BookId == id)
                                 .Include(b => b.Reviews)
                                 .AsNoTracking()
                                 .FirstOrDefault();
    

    But there is a big chance that your navigation property will not be populated (because it is handled by change tracking). In such case use this approach:

    var book = db.Books.Where(b => b.BookId == id).AsNoTracking().FirstOrDefault();
    book.Reviews = db.Reviews.Where(r => r.BookId == id).AsNoTracking().ToList();
    

    Anyway can you see what object type is passed to Equals? I think it should compare only primary keys and even 50M integer comparisons should not be such a problem.

    As a side note EF is slow – it is well known fact. It also uses reflection internally when materializing entities so simply 10.000 records can take “some time”. Unless you already did that you can also turn off dynamic proxy creation (db.Configuration.ProxyCreationEnabled).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into an interesting issue with the relatively simple assignment below. Each of
I've run into some interesting code in our legacy application running under Internet Explorer.
I'm running into an issue where I have a FileUpload control in an UpdatePanel.
I'm running into a mental roadblock here and I'm hoping that I'm missing something
I'm running into a common pattern in the code that I'm writing, where I
I am running into an interesting error with my WCF JSONP web service. It
I'm running into an interesting problem in Powershell, and haven't been able to find
I have ran into an interesting issue while trying to create a more usable
I ran into an interesting error with the following LiNQ query using LiNQPad and
Running into a recursion issue. Firebug says line 600: // Recurse if we're merging

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.