首页 > 解决方案 > 上传到 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);
    }

标签: database.net-coreentity-framework-coreef-core-3.0

解决方案


推荐阅读