database - 上传到 Azure 时 EF Core 性能下降
问题描述
我目前使用的是 EFCore 3.0。我有一个查询,它从数据库中获取记录,这些记录在 localhost 中运行一秒钟,但在 Azure Web 服务上运行超过 10 秒。
我编写的代码是根据他们的连接从数据库中检索所有用户帖子,类似于社交墙帖子的想法。postheaders 首先作为 IQueryable 检索,然后根据页码和页面大小处理为 IEnumerable。
PostHeaders 中有数百条记录。这是在 EFCore 中处理数据查询的正确方法吗?
这是我从数据库中检索帖子的 EF 代码:
public async Task<PagedList<PostHeader>> GetPosts(int socialProfileId, SocialPostHeaderParams socialPostHeaderParams)
{
List<int> userConnectionIds = await _context.SocialConnections
.Where(sc => sc.FirstSUPId == socialProfileId && sc.Status == "Connected")
.Select(sc => sc.SecondSUPId)
.ToListAsync();
userConnectionIds.Add(socialProfileId);
IQueryable<PostHeader> postHeadersToReturn = _context.PostHeaders
.Include(u => u.SocialUserProfile)
.ThenInclude(sup => sup.SocialAlbums)
.ThenInclude(sa => sa.SocialPhotos)
.Include(ps => ps.PostStatus)
.ThenInclude(psp => psp.PostPictures)
.Include(pc => pc.PostComments)
.Include(ph => ph.PostHearts)
.Where(ph => (userConnectionIds.Contains(ph.CreatedBySUPId) && ph.SocialGroupId == 0 && ph.SocialPageId == 0 && ph.Enabled))
.OrderByDescending(ph => ph.Created)
.AsQueryable();
return await PagedList<PostHeader>.CreateAsync(postHeadersToReturn, socialPostHeaderParams.PageNumber, socialPostHeaderParams.PageSize);
}
public static async Task<PagedList<T>> CreateAsync(IQueryable<T> source, int pageNumber, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageNumber -1) * pageSize).Take(pageSize).ToListAsync();
return new PagedList<T>(items, count, pageNumber, pageSize);
}
解决方案
推荐阅读
- 3d - 如何根据点的类别制作 3D 密度图
- matlab - 为什么用空单元构建 MATLAB 结构对象会创建一个空结构?
- primeng - 带有 filterMatchMode 的 PrimeNG 下拉菜单
- java - 增量稀疏矩阵
- robotframework - 在机器人框架中设置页面加载超时
- ios - 检测 iphone 是刘海屏还是普通矩形屏以避免导航栏大小问题?
- java - 贪心算法 - 没主意
- ruby-on-rails - 在 Rails 模型范围内强制执行特定顺序
- javascript - 在 Angular 中动态传递接口作为参数
- python - 直接从 SFTP 服务器将音频文件加载到 Python 的语音识别模块(使用 Paramiko SFTPClient)