Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework Performance Issue
    primarykey
    data
    text
    <p>I am running into an interesting performance issue with Entity Framework. I am using Code First.</p> <p>Here is the structure of my entities:</p> <p>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.</p> <pre><code>public class Book { public int BookId { get; set; } // ... public ICollection&lt;Review&gt; Reviews { get; set; } } public class Review { public int ReviewId { get; set; } public int BookId { get; set; } public Book Book { get; set; } public ICollection&lt;Comment&gt; Comments { get; set; } } public class Comment { public int CommentId { get; set; } public int ReviewId { get; set; } public Review Review { get; set; } } </code></pre> <p>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:</p> <pre><code>var bookAndReviews = db.Books.Where(b =&gt; b.BookId == id) .Include(b =&gt; b.Reviews) .FirstOrDefault(); </code></pre> <p>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.</p> <p>Using ANTS Performance Profiler it looks like a bulk of the time is being spent doing a few different things:</p> <p><strong>The Equals method is being called 50 million times.</strong></p> <p>Does anyone know why it would need to call this 50 million times and how I could increase the performance for this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload