EF joining 5 large tables - optimization of the Include () method

I have the following database structure (EDMX models):

public class Company 
{
    public Guid Id { get; set; }
    public virtual ICollection<Location> Locations { get; set; }
    // ... 15 more columns
}

public class Location 
{ 
    public Guid Id { get; set; }
    public virtual Company Company { get; set; }
    public Guid CompanyId { get; set; }

    public virtual ICollection<ReportA> ReportsA { get; set; }
    public virtual ICollection<ReportB> ReportsB { get; set; }
    public virtual ICollection<ReportC> ReportsC { get; set; }
    // ... 15 more columns with information - name, description etc.
}

public class ReportA
{
    public virtual Location Location { get; set; }
    public Guid LocationId { get; set; }
    // 30 more columns of type "int?"
}

public class ReportB
{
    public virtual Location Location { get; set; }
    public Guid LocationId { get; set; }
    // 30 more columns of type "int?"
}

public class ReportC
{
    public virtual Location Location { get; set; }
    public Guid LocationId { get; set; }
    // 30 more columns of type "int?"
}

One company can have many places. At each location there are many reports of the A, B and C.
The columns of the tables ReportA, ReportB, ReportCis different. Each of the tables Reporthas approximately 40,000 rows.
Tables Companyand Locationhave ~ 5000 rows

I need to get all the data and make a final report.
The code:

 using (ComapnyEntities dataBaseContext = new ComapnyEntities())
 {
      IQueryable<Locations> query = dataBaseContext.Locations
            .AsNoTracking()
            .Where(location => companyIds.Contains(location.CompanyId))
            .Include(location => location.Company)
            .Include(location => location.ReportsA)
            .Include(location => location.ReportsB)
            .Include(location => location.ReportsC);
       // more filtation 
       return query.ToList();

       // assume that companyIds have all company Ids 
}

In most cases, I can use Skip()and methods Take()that speed up execution (i.e. .Take(10)), but in one specific case I need to pull out all the locations along with company information, ReportsA, ReportsB, ReportsC.

Stackoverflow, Include() . qaru.site/questions/380698/...

 // so if that is true:
 4 000 * 4 000 * 40 000 * 40 000 * 40 000 = ?? (1.024e+21)

" " , 15 (), .

, ?

  • Entity-framework Include() -
  • Non-Clustured Location.CompanyId, ReportA.LocationId, ReportB.LocationId, ReportC.LocationId, LocationId - ( , )
  • SQL-, , 10 .
  • .Inlude() .IncludeOptimized(), .

SQL Server 2014 8.1

? ?

+4
1

.

" " " ". , , .

+4

Source: https://habr.com/ru/post/1678069/


All Articles