title: Entity Framework AsNoTracking on table is faster than directly calling stored procedure through EF
body: <p>I have a situation where if I have a DbSet linked to a table by EF and I query it using LINQ, it is consistently slower than directly using a stored procedure that has been linked up to the stored procedure in the database using the generate model feature of EF.</p>
  
  <p>How could this be?</p>
  
  <p>This is the code for querying the table with EF with AsNoTracking():</p>
  
  <pre><code>var relatedParcels = _db.Parcels
                  .AsNoTracking()
                  .Where(parcel =&gt; parcel.Flag == false)
                  .Where(parcel =&gt; parcel.Id != givenThing.id)
                  .Where(parcel =&gt; parcel.Geometry != null).ToArray()
                  .Where(parcel =&gt; parcel.Geometry.Intersects(bufferedGeometry))
                  .Where(parcel =&gt; parcel.Forecasts.Any(forecast =&gt; forecast.Year == finalYear
                                                            &amp;&amp; (forecast.Area1Id == mainArea
                                                                || forecast.Area2Id == mainArea)))
                  .ToArray();
  
  return relatedParcels .Select(relatedParcel =&gt; relatedParcel.Forecasts
                  .Where(forecast =&gt; forecasts.Year == previousYear)
                  .ToArray());
  
  </code></pre>
  
  <p>This is the stored procedure:</p>
  
  <pre><code>SELECT Forecasts.*
  FROM Forecasts
  WHERE Forecasts.Year = @PreviousYear 
    AND Forecasts.ParcelId IN (SELECT Parcel.Id
                               FROM Parcel
                               INNER JOIN Geometry ON Parcel.GeometryId = Geometry .Id
                               WHERE Parcel.Flag = 0 
                                 AND Parcel.Id != @ParcelId
                                 AND Geometry.CentroidPoint.STIntersects(@NearbyGeometry.STBuffer(@Buffer)) = 1 
                                 AND Parcel.Id IN (SELECT Forecasts.ParcelId
                                                   FROM Forecasts
                                                   WHERE Forecasts.Year = @FinalYear 
                                                     AND (Forecasts.Area1Id = @MainArea 
                                                          OR Forecasts.Area2Id = @MainArea)
  </code></pre>
  
  <p>And this is how it is called:</p>
  
  <pre><code>var relatedParcels = db.RelatedParcels(parcelId, buffer, geometry, finalYear, mainArea, previousYear).GroupBy(x =&gt; x.parcelId);
  </code></pre>
  
  <p>The LINQ query directly to the table is consistently 3 times faster than directly calling the stored procedure.</p>
  
  <p>I am using .NET Framework 4.7.2 and EF 6.</p>
  
  <p>TLDR: how could EF ever be faster than a stored procedure?</p>