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?
virtual. I'm not sure if that's related or not.