From 0ee1ed243675d7fcfd9c8e9acae44f2a12bed040 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 3 Dec 2020 20:29:01 +0330 Subject: [PATCH] trying to improve UpdatePoemsText performance --- RMuseum/Services/Implementation/GanjoorService.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 90f4cc59..bd3d87e3 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -153,6 +153,7 @@ namespace RMuseum.Services.Implementation using (RMuseumDbContext context = new RMuseumDbContext(Configuration)) //this is long running job, so _context might be already been freed/collected by GC { var poems = await context.GanjoorPoems.Where(p => p.PlainText == null).ToListAsync(); + int n = 0; foreach (GanjoorPoem poem in poems) { var verses = await context.GanjoorVerses.Where(v => v.PoemId == poem.Id).OrderBy(v => v.VOrder).ToListAsync(); @@ -216,11 +217,20 @@ namespace RMuseum.Services.Implementation context.Update(poem); - await context.SaveChangesAsync(); + n++; + + if(n >= 100) + { + await context.SaveChangesAsync(); + n = 0; + } } } - + + if(n > 0) + await context.SaveChangesAsync(); + }