patterncsharpMinor
Getting instant/better result instead of waiting at least 10 seconds every time
Viewed 0 times
resultwaitingsecondseverygettingbettertimeinsteadleastinstant
Problem
I have the following code that unfortunately is really slow:
```
private void FilterSessionByDate()
{
SessionsFilteredByDate =
BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects().Where(
i => i.CreatedDate >= GetFromDate() && i.CreatedDate i.TrackerId == CurrentItem.ID);
}
private void BindGrid(DateTime fromDate, DateTime toDate, int trackerId, int campaignId)
{
var result = BusinessClient.Instance.Tracker.GetReportForCampaign(trackerId, campaignId, fromDate, toDate);
foreach (var trackerReport in result)
{
trackerReport.Errors = GetErrors(trackerReport);
}
StatisticsGrid.DataSource = result.Where(r => r.ParentMilestoneId == null);
StatisticsGrid.DataBind();
}
private int GetErrors(TrackerReport milestone)
{
int counter = 0;
//var milestonesWithId = BusinessClient.Instance.Tracker.GetAllMilestonesInSessionWithTrackerId(CurrentItem.ID);
//var sessionsWithDateRange = milestonesWithId.Where(i=> i.CreatedDate >= GetFromDate() && i.CreatedDate i.StatusId == 0);
foreach (var milestonesInSession in sessionsWithStatusError)
{
// Get all milestoneinsessions with this session
MilestonesInSession session = milestonesInSession;
var localPath = BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects().Where(
i => i.SessionId == session.SessionId).ToList();
var lastStep = localPath.Max(i => i.MilestoneId);
var latestTime = localPath.Where(i => i.MilestoneId != lastStep).Max(i=>i.CreatedDate);
var milestoneWithLatestTime = localPath.First(x => x.CreatedDate == latestTime);
if (milestonesInSession.MilestoneId != milestone.MilestoneId)
{
if (milestoneWithLatestTime.MilestoneId == milestone.MilestoneId &&
milestoneWithLatestTime.CreatedDate.AddSeconds(2) >= milestonesInSession.CreatedDate)
counter++;
}
else
{
// It is the last milestone, we have to check if the milestone with
// the latest time is close to
```
private void FilterSessionByDate()
{
SessionsFilteredByDate =
BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects().Where(
i => i.CreatedDate >= GetFromDate() && i.CreatedDate i.TrackerId == CurrentItem.ID);
}
private void BindGrid(DateTime fromDate, DateTime toDate, int trackerId, int campaignId)
{
var result = BusinessClient.Instance.Tracker.GetReportForCampaign(trackerId, campaignId, fromDate, toDate);
foreach (var trackerReport in result)
{
trackerReport.Errors = GetErrors(trackerReport);
}
StatisticsGrid.DataSource = result.Where(r => r.ParentMilestoneId == null);
StatisticsGrid.DataBind();
}
private int GetErrors(TrackerReport milestone)
{
int counter = 0;
//var milestonesWithId = BusinessClient.Instance.Tracker.GetAllMilestonesInSessionWithTrackerId(CurrentItem.ID);
//var sessionsWithDateRange = milestonesWithId.Where(i=> i.CreatedDate >= GetFromDate() && i.CreatedDate i.StatusId == 0);
foreach (var milestonesInSession in sessionsWithStatusError)
{
// Get all milestoneinsessions with this session
MilestonesInSession session = milestonesInSession;
var localPath = BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects().Where(
i => i.SessionId == session.SessionId).ToList();
var lastStep = localPath.Max(i => i.MilestoneId);
var latestTime = localPath.Where(i => i.MilestoneId != lastStep).Max(i=>i.CreatedDate);
var milestoneWithLatestTime = localPath.First(x => x.CreatedDate == latestTime);
if (milestonesInSession.MilestoneId != milestone.MilestoneId)
{
if (milestoneWithLatestTime.MilestoneId == milestone.MilestoneId &&
milestoneWithLatestTime.CreatedDate.AddSeconds(2) >= milestonesInSession.CreatedDate)
counter++;
}
else
{
// It is the last milestone, we have to check if the milestone with
// the latest time is close to
Solution
If BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects() is not returning IQueryable, you will be loading up all results from that method before applying the Where filters - on each iteration through the for loop. If that method really does what it says, then you are talking about transmitting millions of records from the DB before filtering, over and over.
On the other hand, if BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects() is returning IQueryable, then are you sure the underlying table is indexed for a comparison on the SessionId field?
Either way, the ANTS performance readout makes it clear that's the bottle-neck.
On the other hand, if BusinessClient.Instance.Tracker.GetAllMilestonesInSessionObjects() is returning IQueryable, then are you sure the underlying table is indexed for a comparison on the SessionId field?
Either way, the ANTS performance readout makes it clear that's the bottle-neck.
Context
StackExchange Code Review Q#16405, answer score: 6
Revisions (0)
No revisions yet.