Entity Framework performance with/without tracking
I've been running some tests related to EF performance and one thing seems
really weird to me. It looks like I'm getting better performance WITH
tracked queries than without tracking. The DB has almost 100.000 records.
Please, take a look at the following piece of code:
public void run()
{
Console.WriteLine("L2E, tracking: " + L2e(6, true));
Console.WriteLine("L2E, no tracking: " + L2e(6, false));
}
private decimal L2e(int loop, bool tracking)
{
List<decimal> timeResultList = new List<decimal>();
using (HotelDBContext context = new HotelDBContext())
{
Stopwatch sw = new Stopwatch();
for (int i = 0; i < loop; i++)
{
sw.Start();
var query = context.Entity1.Include("Entity2");
if (!tracking)
query = query.AsNoTracking();
query.ToList();
sw.Stop();
timeResultList.Add(sw.ElapsedMilliseconds / 1000m);
sw.Reset();
}
}
timeResultList.RemoveAt(0);
return timeResultList.Average();
}
The loop runs for 6 times, then I remove the first result (frst use of
context) and average the rest. Results:
96822
L2E, tracking: 0.282
0
L2E, no tracking: 1.404
Why is this faster for entities when tracking is disabled? Or am I missing
some stupid thing in my code...
No comments:
Post a Comment