2
Reply

How AsNoTracking() impact on performance ?

  • Which condition we use & which not.
  • Impact on the application performance.

    In Entity Framework (EF) Core,AsNoTracking() tells the DbContext not to track the entities returned by a query. By default, EF tracks all retrieved objects to automatically detect and save modifications.

    • AsNoTracking() improves performance by 35–40% and reduces memory usage by 50% because it stops the DbContext from snapshotting data or monitoring property changes.

    Simple example:

    // Use for fast, read-only data (API/Reports)
    var list = context.Products.AsNoTracking().ToList();// Skip for updates (requires tracking)
    var item = context.Products.First();
    item.Price = 10;
    context.SaveChanges(); 

    Hope this helps you understand.

    **AsNoTracking()** is a method used to improve read-only query performance by disabling change tracking on the returned entities,**EF** by-default tracks changes that made to entities retrieved from database , allows it to detect any change to those entities and apply changes to db by **SaveChange()** method So by using **AsNoTracking()** EF skips tracking process which make the query more efficient for read only operations so the effects on performance are:1. Increased Query Performance :Improves read performance by reducing memory usage and CPU overhead , works well for read-only scenarios like displaying data 2.Reduce memory usage : EF doesn't store the status of the Entities that are not tracked in change tracker3.Faster Query Execution Since there is no need to track changes, EF skips the process of snapshotting the data and monitoring changes, resulting in faster queries.**Use AsNoTracking() when:**1. You are only reading data and not modifying it.2.Performance is critical.3.The result will not be saved back to the database. **Avoid AsNoTracking() when:**1. You need to modify the data and save changes.2. You require relationship fix-ups (automatically setting navigation properties).