Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why is Equals called 50M times? </p> </blockquote> <p>It sounds quite suspicious. You have 10.000 reviews and 50.000.000 calls to <code>Equals</code>. 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:</p> <pre><code>============================================= 1st record read | 0 comparisons 2nd record read | 1 comparison 3rd record read | 2 comparisons ... 10.000th record read | 9.999 comparisons </code></pre> <p>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":</p> <pre><code>a(n) = a(n-1) + 1 Sum(n) = (n / 2) * (a(1) + a(n)) Sum(10.000) = 5.000 * (0 + 9.999) =&gt; 5.000 * 10.000 = 50.000.000 </code></pre> <p>I hope I didn't make mistake in my assumptions or calculation. Wait! I hope I did mistake because this doesn't seem good.</p> <p>Try turning off change tracking = hopefully turning off identity map checking.</p> <p>It can be tricky. Start with:</p> <pre><code>var bookAndReviews = db.Books.Where(b =&gt; b.BookId == id) .Include(b =&gt; b.Reviews) .AsNoTracking() .FirstOrDefault(); </code></pre> <p>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:</p> <pre><code>var book = db.Books.Where(b =&gt; b.BookId == id).AsNoTracking().FirstOrDefault(); book.Reviews = db.Reviews.Where(r =&gt; r.BookId == id).AsNoTracking().ToList(); </code></pre> <p>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. </p> <p>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 (<code>db.Configuration.ProxyCreationEnabled</code>).</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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