4

I'm having problems with my MVC 4 application running too slowly. I installed Glimpse to profile the application. I think I've found part of the problem: many of my EF queries appear to be running twice! Here's my HomeController that is pulling some alerts:

[HttpGet]
    public virtual ActionResult Index()
    {
        var reportStart = DateTime.Today;
        var reportEnd = DateTime.Today.AddMonths(1);

        var query = _tameUow.Alerts
                            .FindBy(a => a.ApprovalStatusId == (int)AlertApprovalStatusCodes.Approved &&
                                    (a.ScheduledStartDateTime >= reportStart &&
                                     a.ScheduledStartDateTime <= reportEnd), a => a.SubmittedBy, a => a.ApprovalManager, a => a.ApprovalStatus);

        var model = ListAlertsViewModelBuilder.Build(query, null, false, false, false, false);

        model.RequiredViewData = new RequiredViewDataModel("Upcoming Alerts", "These are the upcoming active alerts for the next month.", "Home");
        return View(model);
    }

But when I view the SQL tab in glimpse, it shows the query twice! At first I thought it was just a bug and the same query was being displayed twice, but they have different execution times, so I think that the query is actually being run twice! Also, the little yellow exclamation mark shows up as a warning. I THINK this is warning me that it is a duplicate query...

https://i.stack.imgur.com/OQ4zQ.png enter image description here What's going on here? I see this on all the pages I've tested, I chose this one as an example because it is the simplest. I tried setting a breakpoint on the query, and it is only being hit once.

Here's the VMBuilder:

public static class ListAlertsViewModelBuilder
{
    public static ListAlertsViewModel Build
        (IQueryable<Alert> query
        , string curUserExtId
        , bool showSubmittedDateTime
        , bool showStatus
        , bool showActions
        , bool showOwners)
    {
        var model = new ListAlertsViewModel();

        var alerts = query.Select(a => new AlertDetailsViewModel() {
            AlertId = (int)a.AlertId,
            ApprovalManager = a.ApprovalManager,
            ApprovalManagerExtId = a.ApprovalManagerExtId,
            ApprovalStatus = a.ApprovalStatus,
            ApprovalStatusId = (int)a.ApprovalStatusId,
            Building = a.Building,
            Cause = a.Cause,
            //Comments = a.Comments,
            Impact = a.Impact,
            ScheduledEndDateTime = a.ScheduledEndDateTime,
            ScheduledStartDateTime = a.ScheduledStartDateTime,
            Service = a.Service,
            SubmittedBy = a.SubmittedBy,
            SubmittedByExtId = a.SubmittedByExtId,
            SubmittedDateTime = a.SubmittedDateTime,
            CurrentUserExtId = curUserExtId
        });

        model.ListAlerts = alerts;

        model.ShowSubmittedDateTime = showSubmittedDateTime;
        model.ShowStatus = showStatus;
        model.ShowActions = showActions;
        model.ShowOwners = showOwners;

        return model;
    }
}

And this is the FindBy method I'm using from my repository:

public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = this.Context.Set<T>();
        foreach (var include in includeProperties)
        {
            query.Include(include);
        }

        return query.Where(predicate);
    }
solidau
  • 4,021
  • 3
  • 24
  • 45
  • Can you step through your code while watching the SQL requests? Since you're never explicitly telling your query to execute it should happen when you call Build for your model. It doesn't look like you have any navigation properties that would have needed to be re-queried (which shouldn't produce the same SQL anyway). If you step through, you should see where they are being triggered. – Erik Noren Sep 12 '13 at 20:49
  • I set a break point in the Build() method and it is hit only once – solidau Sep 12 '13 at 22:32
  • Build itself is only hit once? Are you watching the queries hit your database at the same time? – Erik Noren Sep 12 '13 at 22:39
  • Yes, Build is only hit once. I am unable to watch the queries as they come in because I dont have the proper tools to do so... all I can see is after the fact from Glimpse – solidau Sep 12 '13 at 23:11
  • 1
    I can verify that from a Glimpse perspective it only shows that alert icon when it finds the same query executed more than one time. If you are having trouble tracking this down, one option - which is a pain in the ass but will do the trick - is to download the Glimpse source and put a break point in the part that detects the command being executed. Then you could look at the call stack to see where the execution is coming from. Also, from our end it would be good if we could do more to help you find the source without resorting to the above. – anthonyv Sep 17 '13 at 12:58
  • 1
    In your View, are you checking for .Any() on the IEnumerable model? If so, check that you have materialized your query in your controller as this can cause it to be reissued. – ScottE Nov 30 '15 at 15:57

0 Answers0