I noticed that my background job performing worse after each batch. Basically, job takes huge csv file and then updates relevant records in database in batches of 250 records. After each batch I am re-creating db context to keep ObjectContext tidy, but that doesn't help and it looks like just everything is getting slower after each batch. My job does:
- Read 250 records from file
- Create DbContext
- Then for each record
- Get corresponding entity from DB
- Update properties (reflection)
- When 250 records processed - SaveChanges
So when job starts, it takes ~170ms for 4. and ~6ms for 5.
After 50 batches, it takes ~430ms for 4. and ~500ms for 5.
Can someone explain please what is happening here?
Update. As it turned out it wasn't hangfire fault - so I removed all references to hangfire.
The problem seems to be with DbContext, despite I am recreating DbContext processing still takes longer after each batch. Also I missed one thing from 5. - call to dbContext.Entry(entity).Property("Guid").IsModified = false that takes longer each time:
//for each 250 items
var dbContext = new MyDbContext();
dbContext.Configuration.ValidateOnSaveEnabled = false;
foreach (var item in items)
{
var entity = dbContext.MyItems
.Include(x => x.SomeProp) // lots of these
.FirstOrDefault(BuildFilterFrom(item)); //after each batch this taking longer (starting from 170ms and then after 50 batches it takes 430ms)
//set entity propties from item
//...
dbContext.Entry(entity).Property("Guid").IsModified = false //after each batch this also taking longer (starting from 6ms and then after 50 batches it takes 500ms)
}
dbContext.SaveChanges();