我使用以下连接在内存集合和其他实体集合中的linq查询。 我使用了存储库模式,每个存储库都有“GetAll”方法,该方法返回IQueryable集合。如你所见,有多个连接,我想通过一个数据库往返来实现,因为性能问题。 当.ToList被命中时,系统忙了将近5分钟,然后返回结果集。此时内存使用量增长过快,需要很长时间。我对此有何不妥?
var results = from
personnel in _personnelService.GetAll()
join da in DateList on 1 equals 1
join dList in DayList on (int)SqlFunctions.DatePart("weekday", da) equals dList
join unit in _unitService.GetAll() on personnel.MainUnitId equals unit.Id
join pus in _personnelUnitMembership.GetAll() on new { Pid = personnel.Id, dayofweek = dList } equals new { Pid = pus.PersonnelId, dayofweek = pus.WeekDay }
into personDates
from pDatesUnits in personDates.DefaultIfEmpty()
join psm in _personnelShiftMembershipService.GetAll() on personnel.Id equals psm.PersonnelId into PSM
from ppsm in PSM.DefaultIfEmpty()
let todayUnit = pDatesUnits == null ? unit : pDatesUnits.Unit
let todayUnitShift = pDatesUnits == null ? unit : pDatesUnits.ShiftFollower == true ? pDatesUnits.Unit : unit
join usm in _unitShiftMembershipService.GetAll() on todayUnit.Id equals usm.UnitId into PersonnelDateUnitShiftMemberships
from pdum in PersonnelDateUnitShiftMemberships.DefaultIfEmpty()
let todayPersonnelShift = ppsm == null ? pdum.Shift : ppsm.Shift
join calendar in _calendarServie.GetAll() on todayPersonnelShift.CalendarId equals calendar.Id
join calendarDay in _dayService.GetAll() on new { CId = calendar.Id, DayNo = dList } equals new { CId = calendarDay.CalendarId, DayNo = calendarDay.DayNo }
join calendarEvent in _calendarEventService.GetAll().Where(x => x.CauseHoliday == true) on new { CId = calendar.Id, Date = da } equals new { CId = calendarEvent.CalendarId, Date = calendarEvent.EventDate } into calendarEvent
from cEvent in calendarEvent.DefaultIfEmpty()
join pShifts in _periodicShiftService.GetAll() on todayPersonnelShift.Id equals pShifts.Id into PersonnelTodayPeriodicShift
from ptps in PersonnelTodayPeriodicShift.DefaultIfEmpty()
join irShifts in _iregularShiftService.GetAll() on todayPersonnelShift.Id equals irShifts.Id into PersonnelTodayIrregularShift
from itps in PersonnelTodayIrregularShift.DefaultIfEmpty()
let shiftDayNo = (int)(ptps != null ? DbFunctions.DiffDays(da, ptps.ShiftStartDate) % ptps.ShiftPeriod + 1 : 0)
join shiftDay in _shiftDayService.GetAll() on new { ShiftId = todayPersonnelShift.Id, DayNo = shiftDayNo } equals new { ShiftId = shiftDay.ShiftId, DayNo = shiftDay.DayNo }
join span in _shiftDaySpanService.GetAll() on shiftDay.Id equals span.ShiftDayId into Spans
from shDaySpans in Spans.DefaultIfEmpty()
join workingSpans in _workingShiftSpanService.GetAll() on shDaySpans == null ? -1 : shDaySpans.Id equals workingSpans.Id into WorkingSpans
from ws in WorkingSpans.DefaultIfEmpty()
join overTimeShiftSpan in _overtitShiftSpanService.GetAll() on shDaySpans == null ? -1 : shDaySpans.Id equals overTimeShiftSpan.Id into OverTimeSpans
from os in OverTimeSpans.DefaultIfEmpty()
join forbiddenPresentShiftSpan in _forbiddenPresentShiftSpanService.GetAll() on shDaySpans == null ? -1 : shDaySpans.Id equals forbiddenPresentShiftSpan.Id into ForbiddenSpans
from fs in ForbiddenSpans.DefaultIfEmpty()
join unitWorkingShiftSpanSettingOverride in _unitWorkingShiftSpanSettingOverrideService.GetAll() on todayUnit.Id equals unitWorkingShiftSpanSettingOverride.UnitId into UWSSSO
from unitWorkingShiftSpanSettingOverride in UWSSSO.DefaultIfEmpty()
join unitOverTimeShiftSpanSettingOverride in _unitOverTimeShiftSpanSettingOverrideService.GetAll() on todayUnit.Id equals unitOverTimeShiftSpanSettingOverride.UnitId into UOTSSSO
from unitOverTimeShiftSpanSettingOverride in UOTSSSO.DefaultIfEmpty()
join personnelShiftDaySettingOverride in _personnelShiftDaySettingOverrideService.GetAll() on personnel.Id equals personnelShiftDaySettingOverride.PersonnelId into PSDSO
from personnelShiftDaySettingOverride in PSDSO.DefaultIfEmpty()
join personnelOverTimeShiftSpanSettingOverride in _personnelOverTimeShiftSpanSettingOverrideService.GetAll() on personnel.Id equals personnelOverTimeShiftSpanSettingOverride.PersonnelId into POTSSSO
from personnelOverTimeShiftSpanSettingOverride in POTSSSO.DefaultIfEmpty()
join personnelWorkingShiftSpanSettingOverride in _personnelWorkingShiftSpanSettingOverrideService.GetAll() on personnel.Id equals personnelWorkingShiftSpanSettingOverride.PersonnelId into PWSSSO
from personnelWorkingShiftSpanSettingOverride in PWSSSO.DefaultIfEmpty()
let holiday = shiftDay.DayStatus == ShiftDayStatus.Holiday || cEvent != null
group new { da, Personnel = personnel, Unit = unit, PersonDates = personDates, Spans, WS = ws, OS = os, FS = fs, ShiftDayNo = shiftDayNo, ShiftDay = shiftDay, Holiday = holiday, TodayUnit = todayUnit, WorkingSpans, OverTimeSpans, ForbiddenSpans } by da into GroupDate
let firstG = GroupDate.FirstOrDefault()
let spans = firstG.Spans
let personnel = firstG.Personnel
let todayUnit = firstG.TodayUnit
let shiftDayNo = firstG.ShiftDayNo
let mainUnit = personnel.MainUnit
let ws = firstG.WorkingSpans
let os = firstG.OverTimeSpans// Spans.Select(x => x.OverTimeShiftSpan).Where(x => x != null)
let fs = firstG.ForbiddenSpans
// let shiftDaySettingOverride = firstG.personnelShiftDaySettingOverride ?? null
// let shiftDay = firstG.ShiftDay
//let holiday = firstG.Holiday
// let pwsso = firstG.PWSSSO != null ? firstG.PWSSSO.Where(y => spans.Select(x => x.Id).Contains(y.WorkingShiftSpanSettingsOverride.WorkingShiftSpanId)).Select(x => x.WorkingShiftSpanSettingsOverride) : null
// let potssso = firstG.POTSSSO != null ? from POTSSSO in firstG.POTSSSO where spans.Select(x => x.Id).Contains(POTSSSO.OverTimeShiftSpanSettingOverride.OverTimeShiftSpanId) select POTSSSO.OverTimeShiftSpanSettingOverride : null
// let overTimeSetting = potssso != null && potssso.Count() != 0 ? potssso : firstG.UWSSSO != null ? from UOTSSSO in firstG.UOTSSSO where spans.Select(x => x.Id).Contains(UOTSSSO.OverTimeShiftSpanSettingOverride.OverTimeShiftSpanId) select UOTSSSO.OverTimeShiftSpanSettingOverride : null
// let workSpanSetting = pwsso != null && pwsso.Count() != 0 ? pwsso : firstG.UWSSSO != null ? from UWSSSO in firstG.UWSSSO where spans.Select(x => x.Id).Contains(UWSSSO.WorkingShiftSpanSettingsOverride.WorkingShiftSpanId) select UWSSSO.WorkingShiftSpanSettingsOverride : null
select new { p = GroupDate.Key };
我已经记录了生成的sql,它在不到40 Mili秒内执行。 sql查询是:
SELECT
1 AS [C1],
[Project43].[C1] AS [C2]
FROM (SELECT
[Distinct1].[C1] AS [C1],
(SELECT TOP (1)
[Extent23].[MainUnitId] AS [MainUnitId]
FROM [dbo].[Personnel] AS [Extent23]
INNER JOIN (SELECT
[UnionAll14].[C1] AS [C1]
FROM (SELECT
[UnionAll13].[C1] AS [C1]
FROM (SELECT
[UnionAll12].[C1] AS [C1]
FROM (SELECT
[UnionAll11].[C1] AS [C1]
FROM (SELECT
convert(datetime2, '2013-12-31 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable13]
UNION ALL
SELECT
convert(datetime2, '2013-12-30 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable14]) AS [UnionAll11]
UNION ALL
SELECT
convert(datetime2, '2013-12-29 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable15]) AS [UnionAll12]
UNION ALL
SELECT
convert(datetime2, '2013-12-28 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable16]) AS [UnionAll13]
UNION ALL
SELECT
convert(datetime2, '2013-12-27 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable17]) AS [UnionAll14]
UNION ALL
SELECT
convert(datetime2, '2013-12-26 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable18]) AS [UnionAll15] ON 1 = 1
INNER JOIN (SELECT
[UnionAll19].[C1] AS [C1]
FROM (SELECT
[UnionAll18].[C1] AS [C1]
FROM (SELECT
[UnionAll17].[C1] AS [C1]
FROM (SELECT
[UnionAll16].[C1] AS [C1]
FROM (SELECT
4 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable19]
UNION ALL
SELECT
3 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable20]) AS [UnionAll16]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable21]) AS [UnionAll17]
UNION ALL
SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable22]) AS [UnionAll18]
UNION ALL
SELECT
7 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable23]) AS [UnionAll19]
UNION ALL
SELECT
6 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable24]) AS [UnionAll20] ON (DATEPART(weekday, [UnionAll15].[C1])) = [UnionAll20].[C1]
LEFT OUTER JOIN [dbo].[PersonnelUnitMemberShip] AS [Extent24] ON ([Extent23].[Id] = [Extent24].[PersonnelId]) AND ([UnionAll20].[C1] = [Extent24].[WeekDay])
LEFT OUTER JOIN [dbo].[PersonnelShiftMembership] AS [Extent25] ON [Extent23].[Id] = [Extent25].[PersonnelId]
LEFT OUTER JOIN [dbo].[Unit] AS [Extent26] ON [Extent24].[UnitId] = [Extent26].[Id]
LEFT OUTER JOIN [dbo].[UnitShiftMembership] AS [Extent27] ON (CASE WHEN ([Extent24].[Id] IS NULL) THEN [Extent23].[MainUnitId] ELSE [Extent26].[Id] END) = [Extent27].[UnitId]
LEFT OUTER JOIN [dbo].[Shift] AS [Extent28] ON [Extent27].[ShiftId] = [Extent28].[Id]
LEFT OUTER JOIN [dbo].[Shift] AS [Extent29] ON [Extent25].[ShiftId] = [Extent29].[Id]
INNER JOIN [dbo].[Calendar] AS [Extent30] ON (CASE WHEN ([Extent25].[Id] IS NULL) THEN [Extent28].[CalendarId] ELSE [Extent29].[CalendarId] END) = [Extent30].[Id]
INNER JOIN [dbo].[Day] AS [Extent31] ON ([Extent30].[Id] = [Extent31].[CalendarId]) AND ([UnionAll20].[C1] = [Extent31].[DayNo])
LEFT OUTER JOIN [dbo].[CalendarEvent] AS [Extent32] ON ([Extent30].[Id] = [Extent32].[CalendarId]) AND (1 = [Extent32].[CauseHoliday]) AND ([UnionAll15].[C1] = [Extent32].[EventDate])
LEFT OUTER JOIN [dbo].[PeriodicShift] AS [Extent33] ON (CASE WHEN ([Extent25].[Id] IS NULL) THEN [Extent28].[Id] ELSE [Extent29].[Id] END) = [Extent33].[Id]
LEFT OUTER JOIN [dbo].[IrregularShift] AS [Extent34] ON (CASE WHEN ([Extent25].[Id] IS NULL) THEN [Extent28].[Id] ELSE [Extent29].[Id] END) = [Extent34].[Id]
INNER JOIN [dbo].[ShiftDay] AS [Extent35] ON ((CASE WHEN ([Extent25].[Id] IS NULL) THEN [Extent28].[Id] ELSE [Extent29].[Id] END) = [Extent35].[ShiftId]) AND ((CASE WHEN ([Extent33].[Id] IS NOT NULL) THEN ((DATEDIFF (day, [UnionAll15].[C1], [Extent33].[ShiftStartDate])) % [Extent33].[ShiftPeriod]) + 1 ELSE 0 END) = [Extent35].[DayNo])
LEFT OUTER JOIN [dbo].[ShiftDaySpan] AS [Extent36] ON [Extent35].[Id] = [Extent36].[ShiftDayId]
LEFT OUTER JOIN [dbo].[WorkingShiftSpan] AS [Extent37] ON (CASE WHEN ([Extent36].[Id] IS NULL) THEN -1 ELSE [Extent36].[Id] END) = [Extent37].[Id]
LEFT OUTER JOIN [dbo].[OverTimeShiftSpan] AS [Extent38] ON (CASE WHEN ([Extent36].[Id] IS NULL) THEN -1 ELSE [Extent36].[Id] END) = [Extent38].[Id]
LEFT OUTER JOIN [dbo].[ForbiddenPresentShiftSpan] AS [Extent39] ON (CASE WHEN ([Extent36].[Id] IS NULL) THEN -1 ELSE [Extent36].[Id] END) = [Extent39].[Id]
LEFT OUTER JOIN [dbo].[UnitWorkingShiftSpanSettingOverride] AS [Extent40] ON (CASE WHEN ([Extent24].[Id] IS NULL) THEN [Extent23].[MainUnitId] ELSE [Extent26].[Id] END) = [Extent40].[UnitId]
LEFT OUTER JOIN [dbo].[UnitOverTimeShiftSpanSettingOverride] AS [Extent41] ON (CASE WHEN ([Extent24].[Id] IS NULL) THEN [Extent23].[MainUnitId] ELSE [Extent26].[Id] END) = [Extent41].[UnitId]
LEFT OUTER JOIN [dbo].[PersonnelShiftDaySettingOverride] AS [Extent42] ON [Extent23].[Id] = [Extent42].[PersonnelId]
LEFT OUTER JOIN [dbo].[PersonnelOverTimeShiftSpanSettingOverride] AS [Extent43] ON [Extent23].[Id] = [Extent43].[PersonnelId]
LEFT OUTER JOIN [dbo].[PersonnelWorkingShiftSpanSettingOverride] AS [Extent44] ON [Extent23].[Id] = [Extent44].[PersonnelId]
WHERE [Distinct1].[C1] = [UnionAll15].[C1]) AS [C2]
FROM ( SELECT DISTINCT
[UnionAll5].[C1] AS [C1]
FROM [dbo].[Personnel] AS [Extent1]
INNER JOIN (SELECT
[UnionAll4].[C1] AS [C1]
FROM (SELECT
[UnionAll3].[C1] AS [C1]
FROM (SELECT
[UnionAll2].[C1] AS [C1]
FROM (SELECT
[UnionAll1].[C1] AS [C1]
FROM (SELECT
convert(datetime2, '2013-12-31 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
convert(datetime2, '2013-12-30 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
UNION ALL
SELECT
convert(datetime2, '2013-12-29 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]) AS [UnionAll2]
UNION ALL
SELECT
convert(datetime2, '2013-12-28 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable4]) AS [UnionAll3]
UNION ALL
SELECT
convert(datetime2, '2013-12-27 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable5]) AS [UnionAll4]
UNION ALL
SELECT
convert(datetime2, '2013-12-26 00:00:00.0000000', 121) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable6]) AS [UnionAll5] ON 1 = 1
INNER JOIN (SELECT
[UnionAll9].[C1] AS [C1]
FROM (SELECT
[UnionAll8].[C1] AS [C1]
FROM (SELECT
[UnionAll7].[C1] AS [C1]
FROM (SELECT
[UnionAll6].[C1] AS [C1]
FROM (SELECT
4 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable7]
UNION ALL
SELECT
3 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable8]) AS [UnionAll6]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable9]) AS [UnionAll7]
UNION ALL
SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable10]) AS [UnionAll8]
UNION ALL
SELECT
7 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable11]) AS [UnionAll9]
UNION ALL
SELECT
6 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable12]) AS [UnionAll10] ON (DATEPART(weekday, [UnionAll5].[C1])) = [UnionAll10].[C1]
LEFT OUTER JOIN [dbo].[PersonnelUnitMemberShip] AS [Extent2] ON ([Extent1].[Id] = [Extent2].[PersonnelId]) AND ([UnionAll10].[C1] = [Extent2].[WeekDay])
LEFT OUTER JOIN [dbo].[PersonnelShiftMembership] AS [Extent3] ON [Extent1].[Id] = [Extent3].[PersonnelId]
LEFT OUTER JOIN [dbo].[Unit] AS [Extent4] ON [Extent2].[UnitId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[UnitShiftMembership] AS [Extent5] ON (CASE WHEN ([Extent2].[Id] IS NULL) THEN [Extent1].[MainUnitId] ELSE [Extent4].[Id] END) = [Extent5].[UnitId]
LEFT OUTER JOIN [dbo].[Shift] AS [Extent6] ON [Extent5].[ShiftId] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[Shift] AS [Extent7] ON [Extent3].[ShiftId] = [Extent7].[Id]
INNER JOIN [dbo].[Calendar] AS [Extent8] ON (CASE WHEN ([Extent3].[Id] IS NULL) THEN [Extent6].[CalendarId] ELSE [Extent7].[CalendarId] END) = [Extent8].[Id]
INNER JOIN [dbo].[Day] AS [Extent9] ON ([Extent8].[Id] = [Extent9].[CalendarId]) AND ([UnionAll10].[C1] = [Extent9].[DayNo])
LEFT OUTER JOIN [dbo].[CalendarEvent] AS [Extent10] ON ([Extent8].[Id] = [Extent10].[CalendarId]) AND (1 = [Extent10].[CauseHoliday]) AND ([UnionAll5].[C1] = [Extent10].[EventDate])
LEFT OUTER JOIN [dbo].[PeriodicShift] AS [Extent11] ON (CASE WHEN ([Extent3].[Id] IS NULL) THEN [Extent6].[Id] ELSE [Extent7].[Id] END) = [Extent11].[Id]
LEFT OUTER JOIN [dbo].[IrregularShift] AS [Extent12] ON (CASE WHEN ([Extent3].[Id] IS NULL) THEN [Extent6].[Id] ELSE [Extent7].[Id] END) = [Extent12].[Id]
INNER JOIN [dbo].[ShiftDay] AS [Extent13] ON ((CASE WHEN ([Extent3].[Id] IS NULL) THEN [Extent6].[Id] ELSE [Extent7].[Id] END) = [Extent13].[ShiftId]) AND ((CASE WHEN ([Extent11].[Id] IS NOT NULL) THEN ((DATEDIFF (day, [UnionAll5].[C1], [Extent11].[ShiftStartDate])) % [Extent11].[ShiftPeriod]) + 1 ELSE 0 END) = [Extent13].[DayNo])
LEFT OUTER JOIN [dbo].[ShiftDaySpan] AS [Extent14] ON [Extent13].[Id] = [Extent14].[ShiftDayId]
LEFT OUTER JOIN [dbo].[WorkingShiftSpan] AS [Extent15] ON (CASE WHEN ([Extent14].[Id] IS NULL) THEN -1 ELSE [Extent14].[Id] END) = [Extent15].[Id]
LEFT OUTER JOIN [dbo].[OverTimeShiftSpan] AS [Extent16] ON (CASE WHEN ([Extent14].[Id] IS NULL) THEN -1 ELSE [Extent14].[Id] END) = [Extent16].[Id]
LEFT OUTER JOIN [dbo].[ForbiddenPresentShiftSpan] AS [Extent17] ON (CASE WHEN ([Extent14].[Id] IS NULL) THEN -1 ELSE [Extent14].[Id] END) = [Extent17].[Id]
LEFT OUTER JOIN [dbo].[UnitWorkingShiftSpanSettingOverride] AS [Extent18] ON (CASE WHEN ([Extent2].[Id] IS NULL) THEN [Extent1].[MainUnitId] ELSE [Extent4].[Id] END) = [Extent18].[UnitId]
LEFT OUTER JOIN [dbo].[UnitOverTimeShiftSpanSettingOverride] AS [Extent19] ON (CASE WHEN ([Extent2].[Id] IS NULL) THEN [Extent1].[MainUnitId] ELSE [Extent4].[Id] END) = [Extent19].[UnitId]
LEFT OUTER JOIN [dbo].[PersonnelShiftDaySettingOverride] AS [Extent20] ON [Extent1].[Id] = [Extent20].[PersonnelId]
LEFT OUTER JOIN [dbo].[PersonnelOverTimeShiftSpanSettingOverride] AS [Extent21] ON [Extent1].[Id] = [Extent21].[PersonnelId]
LEFT OUTER JOIN [dbo].[PersonnelWorkingShiftSpanSettingOverride] AS [Extent22] ON [Extent1].[Id] = [Extent22].[PersonnelId]
) AS [Distinct1] ) AS [Project43]
LEFT OUTER JOIN [dbo].[Unit] AS [Extent45] ON [Project43].[C2] = [Extent45].[Id]