The following Linq Query joins over 6 tables and creates a List of 'AppointmentData' . Inside the joins , 'Appointment' table and 'Patient' table has the largest data . ( approx 15k for appointments and 5k for patients )
It's taking 50 seconds to execute this code.
IQueryable<Appointment> Appointments;
if (condition1)
{
Appointments = _context.Appointment.Where(somecondition);
}
else
{
Appointments = _context.Appointment.Where(othercondition);
}
AppointmentsData = (
from
app in Appointments
join
pat in _context.Patient
on
app.IdPatient equals pat.Id
join
doc in _context.Doctor
on
app.IdDoctor equals doc.Id
...
...
//* Around 5 more joins of the same type * //
...
select new Models.AppointmentData()
{
Id = app.Id,
Patient = pat.FullName,
Doctor = doc.FullName,
...
...
...
/* around 15 more fields from different tables
that were joined */
.....
}
).ToList();
I've tried using a smaller version of the database , with 2k appointments and 1k patients and it takes less than 3 seconds.
I have omitted a few conditions because they were confusing and I'm sure they're not related to the problem.