包含然后包含性能

时间:2019-03-04 12:26:00

标签: c# entity-framework asp.net-web-api entity-framework-core

我有一个EFCore查询,耗时> 80秒。我在互联网上读到,在单个查询中大量使用“包含”是一个坏主意,因为EFCore创建了一个非常复杂的SQL字符串。

根据互联网上的几次建议,我已经开始将大型的单个查询拆分成较小的部分。

不幸的是,互联网上的大多数示例都非常简单。没有使用ThenInclude的情况。任何人都知道我该怎么做才能减少对“ ThenInclude”的调用或以其他方式重写?

在此版本中,查询仍然非常缓慢:

public async Task<List<TblAppointment>> GetAppointments(System.Guid activityId, System.Guid? categoryOrgId)
{
    var query = _context.TblAppointment
        .Join(_context.TblTour, appointment => appointment.ApmPkId, tour => tour.TouId, (appointment, tour) => new { appointment, tour })
        .Where(find => !find.appointment.ApmDDeleted.HasValue && find.tour.TouOrgId.HasValue && find.tour.TouOrgId.Value == activityId)
        .Select(select => select.appointment);

    await query.Include(include => include.TblAppointmentQuestionnaireResult)
        .ThenInclude(include => include.TblAppointmentQuestionnaireResultAnswer)
        .ThenInclude(include => include.TblAppointmentQuestionnaireResultAnswerHistory)
        .LoadAsync();

    await query.Include(include => include.TblAppointmentQuestionnaireResult)
        .ThenInclude(include => include.TblAppointmentQuestionnaireResultAnswer)
        .ThenInclude(include => include.ApmqstrsanQstgrqs)
        .ThenInclude(include => include.QstgrqsLst)
        .ThenInclude(include => include.TblListEntry)
        .LoadAsync();

    await query.Include(include => include.TblAppointmentStatus)
        .ThenInclude(include => include.ApmstaSta)
        .LoadAsync();

    await query.Include(include => include.TblAppointmentAssignment)
        .ThenInclude(include => include.ApmassOrg)
        .LoadAsync();

    return await query.ToListAsync();
}

0 个答案:

没有答案