EF Query创建了大量子查询

时间:2016-11-03 09:39:30

标签: c# sql-server entity-framework

我有以下数据结构:

ion-content有1对多Meeting

MeetingRooms有1对多MeetingRoom

通过连接表相关:

Meeting

会议还有与会者,定义为:

public class MeetingRoomMeeting : Entity { public Guid MeetingRoomId { get; set; } public virtual MeetingRoom MeetingRoom { get; set; } public Guid MeetingId { get; set; } public virtual Meeting Meeting { get; set; } } 有1对多Meeting

Attendees有1 Attendee

我有两个问题:

获取Meeting

的所有meetings

获取meeting room

中所有attendees的平均meetings

查询执行时间的差异大约为100-200ms,仅用于获取会议室,约30-50秒用于获取与会者。

查询如下:

这将返回会议列表(用于进一步的聚合和转换)。这是一组特定房间(由它的Guid定义)在一组日期之间的会议。

meeting room

它会生成以下查询:

var meetings = _reader.Query()
               .Where(room => room.Id == query.Id)
               .Include(room => room.Meetings)
                   .ThenInclude(meeting => meeting.Meeting)
               .Select(room => new Models.MeetingRoom
               {
               Id = room.Id,
               Name = room.Name,
               Location    = room.Location,
               Meetings = room.Meetings.Where(meeting => meeting.Meeting.StartTime >= query.StartTime && meeting.Meeting.EndTime <= query.EndTime).Select(meeting => new MeetingRoomMeeting
                   {
                       MeetingId = meeting.MeetingId,
                       Meeting = meeting.Meeting
                   }).ToList()
               })
           .FirstOrDefault();

现在罪魁祸首查询是相同的,只是与与会者:

'SELECT [meeting.Meeting].[Id], [meeting.Meeting].[BookedAt], [meeting.Meeting].[DeliveryId], [meeting.Meeting].[EndTime], [meeting.Meeting].[EndedAt], [meeting.Meeting].[LocationId], [meeting.Meeting].[StartTime], [meeting.Meeting].[Subject], [meeting.Meeting].[UpdatedAt], [meeting.Meeting].[WasAdhoc], [meeting.Meeting].[WasCancelled], [meeting.Meeting].[WasEnded], [meeting.Meeting].[WasNoShow], [meeting].[MeetingRoomId], [meeting].[MeetingId]
FROM [MeetingRoomMeeting] AS [meeting]
INNER JOIN [Meeting] AS [meeting.Meeting] ON [meeting].[MeetingId] = [meeting.Meeting].[Id]
WHERE ([meeting.Meeting].[StartTime] >= @__query_StartTime_1) AND ([meeting.Meeting].[EndTime] <= @__query_EndTime_2)',N'@__query_StartTime_1 datetime2(7),@__query_EndTime_2 datetime2(7)',@__query_StartTime_1='2016-10-02 22:00:00',@__query_EndTime_2='2016-11-03 22:59:59'

主要区别在于我必须进入联接表&#34; MeetingRoomMeeting&#34;并找到具体的会议并指定我也希望 var meetings = _reader.Query() .Where(room => room.Id == query.Id) .Include(room => room.Meetings) .ThenInclude(meeting => meeting.Meeting) .Select(room => new Models.MeetingRoom { Id = room.Id, Name = room.Name, Location = room.Location, Meetings = room.Meetings.Where(meeting => meeting.Meeting.StartTime >= query.StartTime && meeting.Meeting.EndTime <= query.EndTime).Select(meeting => new MeetingRoomMeeting { MeetingId = meeting.MeetingId, Meeting = new Models.Meeting { StartTime = meeting.Meeting.StartTime, Attendees = meeting.Meeting.Attendees } }).ToList() }) .FirstOrDefault();

这会产生与上述类似的查询:

Attendees

然后是大量的这些:

N'SELECT [meeting].[MeetingRoomId], [meeting].[MeetingId], [meeting.Meeting].[StartTime], [meeting.Meeting0].[Id]
FROM [MeetingRoomMeeting] AS [meeting]
INNER JOIN [Meeting] AS [meeting.Meeting] ON [meeting].[MeetingId] = [meeting.Meeting].[Id]
INNER JOIN [Meeting] AS [meeting.Meeting0] ON [meeting].[MeetingId] = [meeting.Meeting0].[Id]
WHERE ([meeting.Meeting].[StartTime] >= @__query_StartTime_1) AND ([meeting.Meeting].[EndTime] <= @__query_EndTime_2)',N'@__query_StartTime_1 datetime2(7),@__query_EndTime_2 datetime2(7)',@__query_StartTime_1='2016-10-02 22:00:00',@__query_EndTime_2='2016-11-03 22:59:59'

我确信在构建LINQ查询方面,我做了一件非常禁止的事情。我考虑在参加者上添加MeetingRoom.Id,以便能够使用SELECT [a].[Id], [a].[Internal], [a].[MeetingId], [a].[PersonId], [a].[Rating], [a].[WasOrganizer] FROM [Attendee] AS [a]

来转换查询

0 个答案:

没有答案