Skip to content

Commit 99de388

Browse files
committed
Greatly optimized SQL write performance
1 parent e569934 commit 99de388

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

OpenBioCardServer/Controllers/Classic/ClassicUserController.cs

Lines changed: 1 addition & 6 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+
.AsNoTracking()
3738
.AsSplitQuery()
3839
.Include(p => p.Contacts)
3940
.Include(p => p.SocialLinks)
@@ -91,12 +92,6 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Class
9192

9293
var profile = await _context.Profiles
9394
.AsTracking()
94-
.Include(p => p.Contacts)
95-
.Include(p => p.SocialLinks)
96-
.Include(p => p.Projects)
97-
.Include(p => p.WorkExperiences)
98-
.Include(p => p.SchoolExperiences)
99-
.Include(p => p.Gallery)
10095
.FirstOrDefaultAsync(p => p.Username == username);
10196

10297
if (profile == null)

OpenBioCardServer/Controllers/ProfileController.cs

Lines changed: 43 additions & 26 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+
.AsNoTracking()
3637
.AsSplitQuery()
3738
.Include(p => p.Contacts)
3839
.Include(p => p.SocialLinks)
@@ -69,24 +70,18 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Profi
6970
}
7071

7172
var profile = await _context.Profiles
72-
.Include(p => p.Contacts)
73-
.Include(p => p.SocialLinks)
74-
.Include(p => p.Projects)
75-
.Include(p => p.WorkExperiences)
76-
.Include(p => p.SchoolExperiences)
77-
.Include(p => p.Gallery)
7873
.FirstOrDefaultAsync(p => p.Username == username);
7974

8075
if (profile == null)
8176
{
8277
return NotFound(new { error = "Profile not found" });
8378
}
8479

85-
// 更新基本资料
8680
DataMapper.UpdateProfileEntity(profile, request);
8781

88-
// 清除并替换所有子项
89-
await ReplaceCollectionItemsAsync(profile, request);
82+
await CleanupCollectionsAsync(profile.Id);
83+
84+
await AddNewCollectionsAsync(profile, request);
9085

9186
await _context.SaveChangesAsync();
9287
await transaction.CommitAsync();
@@ -102,7 +97,6 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Profi
10297
}
10398
}
10499

105-
106100
/// <summary>
107101
/// 获取当前登录用户的资料(私有)
108102
/// </summary>
@@ -122,6 +116,7 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
122116
}
123117

124118
var profile = await _context.Profiles
119+
.AsNoTracking()
125120
.AsSplitQuery()
126121
.Include(p => p.Contacts)
127122
.Include(p => p.SocialLinks)
@@ -139,57 +134,79 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
139134
return DataMapper.ToProfileDto(profile);
140135
}
141136

142-
private async Task ReplaceCollectionItemsAsync(ProfileEntity profile, ProfileDto dto)
137+
138+
private async Task CleanupCollectionsAsync(Guid profileId)
139+
{
140+
await _context.ContactItems
141+
.Where(x => x.ProfileId == profileId)
142+
.ExecuteDeleteAsync();
143+
144+
await _context.SocialLinkItems
145+
.Where(x => x.ProfileId == profileId)
146+
.ExecuteDeleteAsync();
147+
148+
await _context.ProjectItems
149+
.Where(x => x.ProfileId == profileId)
150+
.ExecuteDeleteAsync();
151+
152+
await _context.WorkExperienceItems
153+
.Where(x => x.ProfileId == profileId)
154+
.ExecuteDeleteAsync();
155+
156+
await _context.SchoolExperienceItems
157+
.Where(x => x.ProfileId == profileId)
158+
.ExecuteDeleteAsync();
159+
160+
await _context.GalleryItems
161+
.Where(x => x.ProfileId == profileId)
162+
.ExecuteDeleteAsync();
163+
}
164+
165+
/// <summary>
166+
/// 将 DTO 转换为实体并添加到 Context
167+
/// </summary>
168+
private async Task AddNewCollectionsAsync(ProfileEntity profile, ProfileDto dto)
143169
{
144-
// 清除现有集合
145-
_context.ContactItems.RemoveRange(profile.Contacts);
146-
_context.SocialLinkItems.RemoveRange(profile.SocialLinks);
147-
_context.ProjectItems.RemoveRange(profile.Projects);
148-
_context.WorkExperienceItems.RemoveRange(profile.WorkExperiences);
149-
_context.SchoolExperienceItems.RemoveRange(profile.SchoolExperiences);
150-
_context.GalleryItems.RemoveRange(profile.Gallery);
151-
152-
// 添加新的集合
153170
if (dto.Contacts?.Any() == true)
154171
{
155172
var contacts = dto.Contacts
156173
.Select(c => DataMapper.ToContactItemEntity(c, profile.Id));
157-
_context.ContactItems.AddRange(contacts);
174+
await _context.ContactItems.AddRangeAsync(contacts);
158175
}
159176

160177
if (dto.SocialLinks?.Any() == true)
161178
{
162179
var socialLinks = dto.SocialLinks
163180
.Select(s => DataMapper.ToSocialLinkItemEntity(s, profile.Id));
164-
_context.SocialLinkItems.AddRange(socialLinks);
181+
await _context.SocialLinkItems.AddRangeAsync(socialLinks);
165182
}
166183

167184
if (dto.Projects?.Any() == true)
168185
{
169186
var projects = dto.Projects
170187
.Select(p => DataMapper.ToProjectItemEntity(p, profile.Id));
171-
_context.ProjectItems.AddRange(projects);
188+
await _context.ProjectItems.AddRangeAsync(projects);
172189
}
173190

174191
if (dto.WorkExperiences?.Any() == true)
175192
{
176193
var workExperiences = dto.WorkExperiences
177194
.Select(w => DataMapper.ToWorkExperienceItemEntity(w, profile.Id));
178-
_context.WorkExperienceItems.AddRange(workExperiences);
195+
await _context.WorkExperienceItems.AddRangeAsync(workExperiences);
179196
}
180197

181198
if (dto.SchoolExperiences?.Any() == true)
182199
{
183200
var schoolExperiences = dto.SchoolExperiences
184201
.Select(s => DataMapper.ToSchoolExperienceItemEntity(s, profile.Id));
185-
_context.SchoolExperienceItems.AddRange(schoolExperiences);
202+
await _context.SchoolExperienceItems.AddRangeAsync(schoolExperiences);
186203
}
187204

188205
if (dto.Gallery?.Any() == true)
189206
{
190207
var gallery = dto.Gallery
191208
.Select(g => DataMapper.ToGalleryItemEntity(g, profile.Id));
192-
_context.GalleryItems.AddRange(gallery);
209+
await _context.GalleryItems.AddRangeAsync(gallery);
193210
}
194211
}
195212

0 commit comments

Comments
 (0)