Skip to content

Commit 33a083b

Browse files
committed
Greatly optimized SQL read performance
1 parent 4d4a367 commit 33a083b

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

OpenBioCardServer/Controllers/Classic/ClassicUserController.cs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public async Task<IActionResult> GetProfile(string username)
3434
try
3535
{
3636
var profile = await _context.Profiles
37+
.AsSplitQuery()
3738
.Include(p => p.Contacts)
3839
.Include(p => p.SocialLinks)
3940
.Include(p => p.Projects)
@@ -87,6 +88,7 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Class
8788
}
8889

8990
var profile = await _context.Profiles
91+
.AsTracking()
9092
.Include(p => p.Contacts)
9193
.Include(p => p.SocialLinks)
9294
.Include(p => p.Projects)
@@ -102,50 +104,75 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Class
102104

103105
// Update basic profile fields
104106
ClassicMapper.UpdateProfileFromClassic(profile, request);
105-
107+
106108
// Clear all existing collections (we'll replace them completely)
107-
_context.ContactItems.RemoveRange(profile.Contacts);
108-
_context.SocialLinkItems.RemoveRange(profile.SocialLinks);
109-
_context.ProjectItems.RemoveRange(profile.Projects);
110-
_context.WorkExperienceItems.RemoveRange(profile.WorkExperiences);
111-
_context.SchoolExperienceItems.RemoveRange(profile.SchoolExperiences);
112-
_context.GalleryItems.RemoveRange(profile.Gallery);
109+
// _context.ContactItems.RemoveRange(profile.Contacts);
110+
// _context.SocialLinkItems.RemoveRange(profile.SocialLinks);
111+
// _context.ProjectItems.RemoveRange(profile.Projects);
112+
// _context.WorkExperienceItems.RemoveRange(profile.WorkExperiences);
113+
// _context.SchoolExperienceItems.RemoveRange(profile.SchoolExperiences);
114+
// _context.GalleryItems.RemoveRange(profile.Gallery);
115+
116+
117+
await _context.ContactItems
118+
.Where(c => c.ProfileId == profile.Id)
119+
.ExecuteDeleteAsync();
120+
121+
await _context.SocialLinkItems
122+
.Where(s => s.ProfileId == profile.Id)
123+
.ExecuteDeleteAsync();
124+
125+
await _context.ProjectItems
126+
.Where(p => p.ProfileId == profile.Id)
127+
.ExecuteDeleteAsync();
128+
129+
await _context.WorkExperienceItems
130+
.Where(w => w.ProfileId == profile.Id)
131+
.ExecuteDeleteAsync();
132+
133+
await _context.SchoolExperienceItems
134+
.Where(s => s.ProfileId == profile.Id)
135+
.ExecuteDeleteAsync();
136+
137+
await _context.GalleryItems
138+
.Where(g => g.ProfileId == profile.Id)
139+
.ExecuteDeleteAsync();
113140

114141
// Add new collections from request
115142
if (request.Contacts?.Any() == true)
116143
{
117144
var contacts = ClassicMapper.ToContactEntities(request.Contacts, profile.Id);
118-
_context.ContactItems.AddRange(contacts);
145+
await _context.ContactItems.AddRangeAsync(contacts);
119146
}
120147

121148
if (request.SocialLinks?.Any() == true)
122149
{
123150
var socialLinks = ClassicMapper.ToSocialLinkEntities(request.SocialLinks, profile.Id);
124-
_context.SocialLinkItems.AddRange(socialLinks);
151+
await _context.SocialLinkItems.AddRangeAsync(socialLinks);
125152
}
126153

127154
if (request.Projects?.Any() == true)
128155
{
129156
var projects = ClassicMapper.ToProjectEntities(request.Projects, profile.Id);
130-
_context.ProjectItems.AddRange(projects);
157+
await _context.ProjectItems.AddRangeAsync(projects);
131158
}
132159

133160
if (request.WorkExperiences?.Any() == true)
134161
{
135162
var workExperiences = ClassicMapper.ToWorkExperienceEntities(request.WorkExperiences, profile.Id);
136-
_context.WorkExperienceItems.AddRange(workExperiences);
163+
await _context.WorkExperienceItems.AddRangeAsync(workExperiences);
137164
}
138165

139166
if (request.SchoolExperiences?.Any() == true)
140167
{
141168
var schoolExperiences = ClassicMapper.ToSchoolExperienceEntities(request.SchoolExperiences, profile.Id);
142-
_context.SchoolExperienceItems.AddRange(schoolExperiences);
169+
await _context.SchoolExperienceItems.AddRangeAsync(schoolExperiences);
143170
}
144171

145172
if (request.Gallery?.Any() == true)
146173
{
147174
var gallery = ClassicMapper.ToGalleryEntities(request.Gallery, profile.Id);
148-
_context.GalleryItems.AddRange(gallery);
175+
await _context.GalleryItems.AddRangeAsync(gallery);
149176
}
150177

151178
await _context.SaveChangesAsync();

OpenBioCardServer/Controllers/ProfileController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public ProfileController(
3333
public async Task<ActionResult<ProfileDto>> GetProfile(string username)
3434
{
3535
var profile = await _context.Profiles
36+
.AsSplitQuery()
3637
.Include(p => p.Contacts)
3738
.Include(p => p.SocialLinks)
3839
.Include(p => p.Projects)
@@ -108,6 +109,7 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
108109
}
109110

110111
var profile = await _context.Profiles
112+
.AsSplitQuery()
111113
.Include(p => p.Contacts)
112114
.Include(p => p.SocialLinks)
113115
.Include(p => p.Projects)

0 commit comments

Comments
 (0)