I had a table which has more than 200,000 records for any particular month.
Getting records from a table is not a problem it is working as expected but searching through records shows very slow performance
var listEmpShiftDetails =ctx.tblTO_ShiftSchedule
.Where(m => m.CompanyId == companyId &&
m.ShiftDate >= fromdate &&
m.ShiftDate <= todate)
.Select(m => m).ToList();
Records fetched from database around: 200 000
var data = (from a in ctx.tblEmployee
join b in ctx.tblTO_Entry on a.Id equals b.EmployeeId
where a.CompanyId == companyId && b.CompanyId == companyId &&
(b.Entry_Date >= fromDate && b.Entry_Date <= toDate)
select new { a, b }).ToList();
*ote: No database called are made in below code.all the data is fetched above
Linq Query to fetch one by one record
foreach (var item in data) // Data consist of employee details 3k Records
{
if (listEmpShiftDetails
.Any(m => m.EmployeeId == item.a.Id &&
m.ShiftDate == item.b.Entry_Date))
{
var shiftDetails = listEmpShiftDetails
.Where(m => m.EmployeeId == item.a.Id &&
m.ShiftDate ==item.b.Entry_Date)
.Select(m => m)
.FirstOrDefault();
//Other Calculations
}
}
Above 2 Lines takes too much time to execute, below is output from Visual Studio. How to improve the performance?
Profiler Output