0

When I am running a Model Mapping, one company has a lot of members, like 405,000 members.

viewModel.EmployeeCount = company.MembershipUser.Count(x => x.Deleted == false);

When I run the SQL query, it takes a few milliseconds. In ASP.NET MVC, EF6 C# this can take up to 10 minutes for one list view controller hit. Thoughts?

Company is my Domain Model Entity, and MembershipUser is a public virtual virtual (FK) using entity framework 6, not C#6

When I'm in my CompanyController (MVC) and I ask for a company list, I get a list without the company count included. When I do a viewModelMapping to my Model to prep to pass to the view, I need to add the count, and do not have access to the context or DB, etc.

            // Redisplay list of companies
            var viewModel = CrmViewModelMapping.CompanyListToCompanyViewModel(pagedCompanyList);

CompanyListToCompanyViewModel maps the list of companies to the list of my ViewModel and does the count (MembershipUsers) there.

I also tried adding the count property to the company DomainModel such as:

public int EmployeeCount
    {
        get
        {
            // return MembershipUser.Where(x => x.Deleted == false).Count();
            return MembershipUser.Count(x => x.Deleted == false);
        }
    }

But it also takes a long time on companies with a lot of Employees.

It's almost like I want this to be my SQL Query:

Select *, (SELECT count(EmployeeID) as Count WHERE Employee.CompanyID = CompanyID) as employeeCount from Company

But early on I just assumed I could let EF lazy loading and subQueries do the work. but the overhead on large counts is killing me. On small datasets I see no real difference, but once the counts get large my site is unsusable.

5
  • What's company? Also, try to log what query is getting executed on the server. Notice that ASP.NET MVC 6 does not exist nor it will ever. May 26, 2018 at 19:15
  • 4
    See what SQL is actually being issued. Perhaps, since this is a navigation property, and not a DbSet, all the users for the company is loaded from the server, and then counted in your application? Try what happens if you go to the DbSet for MemberUser and do a Count(x => x.CompanyId == company.Id && x.Deleted == false).
    – gnud
    May 26, 2018 at 19:29
  • When I run the SQL query => The one generated by EF? May 26, 2018 at 19:31
  • What is the type of MembershipUser property?
    – Evk
    May 26, 2018 at 20:41
  • MembershipUser is fk connected Domain Model Entity, MembershipUser has a company_companyID key
    – ransems
    May 27, 2018 at 11:53

1 Answer 1

1

When you are accessing the navigation property and using the count method, you are materializing all the MembershipUser table and doing the filter in C#.

There are three operations in this command: The C# go to the database and execute the query, transform the query result in C# object list (materialize) and execute the filter (x => x.Deleted == false) in this list.

To solve this problem you can do the filter in the MembershipUser DbSet:

Db.MembershipUser.Count(x => x.Deleted == false && companyId == company.Id);

Doing the query using the DbSet, the filter will be done in database without materialize all 405000 rows.

5
  • This make a lot of sense. And I see why this query goes off and does some work and comes back 10 minutes later. But I am not sure how to implement in code. When I'm on my controller (MVC) and I ask for a company list, I get a list without the company count included. When I do a viewModelMapping to my Model to prep to pass to the view, I need to add the count, and do not have access to the context or DB, etc.
    – ransems
    May 27, 2018 at 12:06
  • @ransems, isn't the adding the context or db only a matter of including a namespace to the scope?
    – user4649737
    May 27, 2018 at 12:24
  • It's so far abstracted when I'm passing data from controller, using an IOC container, then passing to a viewModelMapping function which has no idea about db's or connections's or db's connections
    – ransems
    May 27, 2018 at 12:27
  • It's almost Like i need my CompanyService (which does the db queries) to return the full company entity and have the counts of employees already included in the SQL query.
    – ransems
    May 27, 2018 at 12:32
  • Maybe I can loop through each company at the CompanyService (my db access service) and 'add' a count, at least it only has to go to the db 25 (max page size) or so more times separately to get the count - but the count would be executed by SQL and not needing to materialize the 405000 entities connected.
    – ransems
    May 27, 2018 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.