I have a query which it need to run 300+ loops every single call. It takes about 10 seconds to complete a call even on a new database. It's unacceptable for a WebAPI call.
var isAbnormal = false;
var list = new List<String>();
//Check date range, log them & return if there is any abnormal.
foreach (DateTime day in DateHelper.EachDay(startDate, endDate))
{
var isActive = db.Operations.Any(x=>x.IsActive && x.Day == day);
var object;
var queryable = db.ObjectA.Where(x=>x.Day == day);
if(isActive){
queryable = db.ObjectA.First(x=>x.Day == day);
LogUtil.Info($"{object.Name}");
var isLogicACorrect = queryable.Any(x=>x.ObjectACount == 5);
var isLogicBCorrect = queryable.Any(x=>x.ObjectBCount == 3);
var isLogicCCorrect = queryable.Any(x=>x.ObjectCCount == 2);
var isLogicDCorrect = queryable.Any(x=>x.ObjectDCount == 8);
var isLogicECorrect = queryable.Any(x=>x.ObjectECount == 1);
if(!isLogicACorrect){
list.Add("Logic A is incorrect");
isAbnormal = true;
}
//More logic codes & db calls here, which is just to select & validate.
}
return list;
How can I optimize the speed by combining all the queries into one? The loop content literally the same except the day.There are total 15 queries to be call on each loop, 4500 db queries in a complete loop.
continue;
? We need to see more to determine what can be changedobject
? You have not assigned anything to it and you are trying toLogUtil.Info($"{object.Name}");
, why would that work? See the issue is with your solution. If you cannot explain your solution (algorithm) in plain English, don't try and code it.