From 532314abbabcc4884f4c00afe71ffb8fd2d5eebd Mon Sep 17 00:00:00 2001 From: Aghogho Bernard Date: Wed, 28 Sep 2022 22:10:24 +0400 Subject: [PATCH 1/5] optimized authors --- BenchmarkService.cs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..db89bd9 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -88,9 +88,48 @@ public List GetAuthors() [Benchmark] public List GetAuthors_Optimized() { - List authors = new List(); + using var dbContext = new AppDbContext(); + var authors = dbContext.Authors + .AsNoTracking() + .Include(x => x.User) + .ThenInclude(x => x.UserRoles) + .ThenInclude(x => x.Role) + .Include(x => x.Books) + .ThenInclude(x => x.Publisher) + .Where(x => x.Country == "Serbia" && x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Select(x => new AuthorDTO + { + UserCreated = x.User.Created, + UserEmailConfirmed = x.User.EmailConfirmed, + UserFirstName = x.User.FirstName, + UserLastActivity = x.User.LastActivity, + UserLastName = x.User.LastName, + UserEmail = x.User.Email, + UserName = x.User.UserName, + UserId = x.User.Id, + RoleId = x.User.UserRoles.FirstOrDefault(y => y.UserId == x.UserId).RoleId, + BooksCount = x.BooksCount, + AllBooks = x.Books.Select(y => new BookDto + { + Id = y.Id, + Name = y.Name, + Published = y.Published, + ISBN = y.ISBN, + PublisherName = y.Publisher.Name, + PublishedYear = y.Published.Year < 1900 ? y.Published.Year : 0 + }).ToList(), + AuthorAge = x.Age, + AuthorCountry = x.Country, + AuthorNickName = x.NickName, + Id = x.Id + }).Take(2).ToList(); + return authors; + //List authors = new List(); + + //return authors; } } } From 769eccba9d9f513ecff89ce616d90dfcb04d8091 Mon Sep 17 00:00:00 2001 From: Aghogho Bernard Date: Wed, 28 Sep 2022 22:22:14 +0400 Subject: [PATCH 2/5] updates --- BenchmarkService.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index db89bd9..348faad 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -125,11 +125,7 @@ public List GetAuthors_Optimized() Id = x.Id }).Take(2).ToList(); - return authors; - //List authors = new List(); - - //return authors; } } } From 5cd84bdd2747412aeda7b6652e81ca4d41fa0908 Mon Sep 17 00:00:00 2001 From: Aghogho Bernard Date: Thu, 29 Sep 2022 22:46:51 +0400 Subject: [PATCH 3/5] missing instructions on the correct projection --- BenchmarkService.cs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 348faad..d921737 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -92,36 +92,23 @@ public List GetAuthors_Optimized() var authors = dbContext.Authors .AsNoTracking() .Include(x => x.User) - .ThenInclude(x => x.UserRoles) - .ThenInclude(x => x.Role) .Include(x => x.Books) .ThenInclude(x => x.Publisher) .Where(x => x.Country == "Serbia" && x.Age == 27) .OrderByDescending(x => x.BooksCount) .Select(x => new AuthorDTO { - UserCreated = x.User.Created, - UserEmailConfirmed = x.User.EmailConfirmed, UserFirstName = x.User.FirstName, - UserLastActivity = x.User.LastActivity, UserLastName = x.User.LastName, - UserEmail = x.User.Email, UserName = x.User.UserName, - UserId = x.User.Id, - RoleId = x.User.UserRoles.FirstOrDefault(y => y.UserId == x.UserId).RoleId, - BooksCount = x.BooksCount, + UserEmail = x.User.Email, + AuthorAge = x.Age, + AuthorCountry = x.Country, AllBooks = x.Books.Select(y => new BookDto { - Id = y.Id, Name = y.Name, - Published = y.Published, - ISBN = y.ISBN, - PublisherName = y.Publisher.Name, PublishedYear = y.Published.Year < 1900 ? y.Published.Year : 0 }).ToList(), - AuthorAge = x.Age, - AuthorCountry = x.Country, - AuthorNickName = x.NickName, Id = x.Id }).Take(2).ToList(); From 71ae0de5424ae8af7f8937880fde9a13d2bfa98c Mon Sep 17 00:00:00 2001 From: Aghogho Bernard Date: Thu, 29 Sep 2022 22:52:06 +0400 Subject: [PATCH 4/5] missing instructions on the correct projection --- BenchmarkService.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d921737..58a7b55 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -104,12 +104,11 @@ public List GetAuthors_Optimized() UserEmail = x.User.Email, AuthorAge = x.Age, AuthorCountry = x.Country, - AllBooks = x.Books.Select(y => new BookDto + AllBooks = x.Books.Where(b => b.Published.Year < 1900).Select(y => new BookDto { Name = y.Name, - PublishedYear = y.Published.Year < 1900 ? y.Published.Year : 0 + PublishedYear = y.Published.Year }).ToList(), - Id = x.Id }).Take(2).ToList(); return authors; From adc54c8e25187ff81a1ee97444ac40a5efc04d50 Mon Sep 17 00:00:00 2001 From: Aghogho Bernard Date: Tue, 4 Oct 2022 18:12:03 +0400 Subject: [PATCH 5/5] more improvements --- BenchmarkService.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 58a7b55..8cb66c8 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -91,9 +91,6 @@ public List GetAuthors_Optimized() using var dbContext = new AppDbContext(); var authors = dbContext.Authors .AsNoTracking() - .Include(x => x.User) - .Include(x => x.Books) - .ThenInclude(x => x.Publisher) .Where(x => x.Country == "Serbia" && x.Age == 27) .OrderByDescending(x => x.BooksCount) .Select(x => new AuthorDTO