包括花费很长的执行时间,我试图将其分成多个块,但仍然需要花费大量记录。
var logbookEntriesFromDbQuery = ctx.LogbookEntries
.AsNoTracking()
.Include(le => le.PatientDetail)
.Include(le => le.AlternateSupervisor)
.Include(le => le.Hospital)
.Include(le => le.Logbook.LogbookType.Specialty)
.Include(le => le.Logbook.LogbookType.Fields)
.Include(le => le.LogbookEntryProcedures.Select(lep => lep.Procedure.ProcedureLogbookTypes.Select(plt => plt.Categories)))
.Include(le => le.LogbookEntryProcedures.Select(lep =>
lep.LogbookEntryProcedureFields.Select(lepf => lepf.ProcedureField).Select(pf => pf.ProcedureProcedureFields)))
.Include(le => le.LogbookEntryProcedures.Select(lep =>
lep.LogbookEntryProcedureFields.Select(lepf => lepf.ProcedureField)
.Select(pf => pf.ProcedureLogbookTypeProcedureFields)))
.Include(le => le.LogbookEntryDiagnoses.Select(led => led.Diagnosis.DiagnosisLogbookTypes))
.Include(le => le.LogbookEntryDiagnoses.Select(led =>
led.LogbookEntryDiagnosisProcedureFields.Select(ledpf => ledpf.ProcedureField)
.Select(pf => pf.DiagnosisProcedureFields)))
.Include(le => le.LogbookEntryDiagnoses.Select(led =>
led.LogbookEntryDiagnosisProcedureFields.Select(ledpf => ledpf.ProcedureField)
.Select(pf => pf.DiagnosisLogbookTypeProcedureFields)))
.Include(le => le.LogbookEntryProcedures.Select(lep => lep.LogbookEntryProcedureComplications))
.Include(le => le.LogbookEntryStatus)
.Include(le => le.LogbookEntryComments)
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo);
答案 0 :(得分:1)
这是因为具有多个Includes会在查询中生成多个JOINS,而联接通常是一个非常耗时的过程。如果您确实需要加载所有信息(例如在表中显示),一个不错的选择是在数据库中创建一个View,如果您不需要一次显示所有信息,则可以取出仅在需要使用时分别包含并获取每个字段,希望这对您有帮助
答案 1 :(得分:-1)
我在下面尝试过,但仍然花费了太长时间。
var logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.PatientDetail)
.Include(le => le.AlternateSupervisor)
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.Hospital)
.Include(le => le.Logbook.LogbookType.Specialty)
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.Logbook.LogbookType.Fields)
.Include(le => le.LogbookEntryProcedures.Select(lep => lep.Procedure.ProcedureLogbookTypes.Select(plt => plt.Categories)))
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.LogbookEntryProcedures.Select(lep =>
lep.LogbookEntryProcedureFields.Select(lepf => lepf.ProcedureField).Select(pf => pf.ProcedureProcedureFields)))
.Include(le => le.LogbookEntryProcedures.Select(lep =>
lep.LogbookEntryProcedureFields.Select(lepf => lepf.ProcedureField)
.Select(pf => pf.ProcedureLogbookTypeProcedureFields)))
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.LogbookEntryDiagnoses.Select(led => led.Diagnosis.DiagnosisLogbookTypes))
.Include(le => le.LogbookEntryDiagnoses.Select(led =>
led.LogbookEntryDiagnosisProcedureFields.Select(ledpf => ledpf.ProcedureField)
.Select(pf => pf.DiagnosisProcedureFields)))
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.LogbookEntryDiagnoses.Select(led =>
led.LogbookEntryDiagnosisProcedureFields.Select(ledpf => ledpf.ProcedureField)
.Select(pf => pf.DiagnosisLogbookTypeProcedureFields)))
.Include(le => le.LogbookEntryProcedures.Select(lep =>
lep.LogbookEntryProcedureComplications))
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();
logbookEntriesFromDb = await ctx.LogbookEntries
.Include(le => le.LogbookEntryStatus)
.Include(le => le.LogbookEntryComments)
.Where(le => le.LogbookId == logbookId)
.Where(le => le.LogbookEntryDate > thirtyDaysAgo)
.ToListAsync();