EF core 2.1 for many-to-many table results in n+1 queries
Details in separate repo here
See execution plan and result query. I believe it should result in one query.
Further technical details
EF Core version: 2.1.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.1.0 Operating system: Windows 10 IDE: Visual Studio 2017 15.8.0
So I was editing query step by step and found the reason:
tickets = await query
.OrderBy(input.Sorting ?? "ticket.id asc") // <=
.PageBy(input)
.ToListAsync();
It's caused by System.Linq.Dynamic.Core.OrderBy()
When I put any order (even System.Linq.OrderBy) EF creates n+1 requests.
Can you create full query without AM & DL and post it here? I looked at the query in AM and it does not contain any OrderBy.
Original full query:
var query = _ticketRepository.GetAll() // IQueryable<Ticket>
/*
.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => false || e.Title.Contains(input.Filter))
.WhereIf(!string.IsNullOrWhiteSpace(input.TitleFilter), e => e.Title.ToLower() == input.TitleFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.TenantNameFilter), e => e.Tenant.Name.ToLower() == input.TenantNameFilter.ToLower().Trim())
.WhereIf(input.MinPlanDateFilter != null, e => e.PlanDate >= input.MinPlanDateFilter)
.WhereIf(input.MaxPlanDateFilter != null, e => e.PlanDate <= input.MaxPlanDateFilter)
.WhereIf(input.MinCloseDateFilter != null, e => e.CloseDate >= input.MinCloseDateFilter)
.WhereIf(input.MaxCloseDateFilter != null, e => e.CloseDate <= input.MaxCloseDateFilter)
.WhereIf(input.MinTicketValueFilter != null, e => e.TicketValue >= input.MinTicketValueFilter)
.WhereIf(input.MaxTicketValueFilter != null, e => e.TicketValue <= input.MaxTicketValueFilter)
.WhereIf(input.IsPrivateFilter > -1, e => Convert.ToInt32(e.IsPrivate) == input.IsPrivateFilter)
.WhereIf(input.IsPaidFilter > -1, e => Convert.ToInt32(e.IsPaid) == input.IsPaidFilter)
.WhereIf(input.IsPendingHostFilter > -1, e => Convert.ToInt32(e.IsPendingHost) == input.IsPendingHostFilter)
*/
.Include(p => p.Product)
.Include(pv => pv.ProductVersion)
.Include(tt => tt.TicketType)
.Include(tp => tp.TicketPriority)
.Include(ts => ts.TicketState)
.Include(ten => ten.Tenant)
.ThenInclude(ed => ed.Edition)
.Include(lu => lu.LinkedUsers)
.ThenInclude(u => u.User)
.Include(lu => lu.LinkedUsers)
.ThenInclude(tlt => tlt.TicketLinkType)
.Select(dto => new GetTicketForView()
{
Tenant = ((dto.Tenant != null) ? new TenantShortInfoDto()
{
TenancyName = dto.Tenant.TenancyName,
Name = dto.Tenant.Name,
Edition = ((dto.Tenant.Edition != null) ? new EditionShortInfoDto()
{
DisplayName = dto.Tenant.Edition.DisplayName
} : null)
} : null),
ProductName = dto.Product.Name,
ProductVersionName = dto.ProductVersion.Name,
TicketState = ((dto.TicketState != null) ? new TicketStateTableDto()
{
Name = dto.TicketState.Name,
Description = dto.TicketState.Description,
Color = dto.TicketState.Color
} : null),
TicketType = ((dto.TicketType != null) ? new TicketTypeTableDto()
{
Name = dto.TicketType.Name,
Description = dto.TicketType.Description
} : null),
TicketPriority = ((dto.TicketPriority != null) ? new TicketPriorityTableDto()
{
Name = dto.TicketPriority.Name,
Description = dto.TicketPriority.Description
} : null),
LinkedUsers = dto.LinkedUsers.Select(dto2 => new TicketLinkedUserDto()
{
User = ((dto2.User != null) ? new UserShortInfoDto()
{
Name = dto2.User.Name,
Surname = dto2.User.Surname,
EmailAddress = dto2.User.EmailAddress,
PhoneNumber = dto2.User.PhoneNumber,
ProfilePictureId = dto2.User.ProfilePictureId,
Id = dto2.User.Id
} : null),
UserId = dto2.UserId,
TicketLinkType = ((dto2.TicketLinkType != null) ? new TicketLinkTypeDto()
{
Code = dto2.TicketLinkType.Code,
Name = dto2.TicketLinkType.Name,
Description = dto2.TicketLinkType.Description,
Ordinal = dto2.TicketLinkType.Ordinal,
IsVisible = dto2.TicketLinkType.IsVisible
} : null),
TicketLinkTypeId = dto2.TicketLinkTypeId,
IsNotify = dto2.IsNotify
}).ToList(),
Ticket = ((dto != null) ? new TicketDto()
{
Title = dto.Title,
PlanDate = dto.PlanDate,
LastModificationTime = dto.LastModificationTime,
CloseDate = (dto.CloseDate ?? DateTime.MinValue),
TicketValue = dto.TicketValue,
IsPrivate = dto.IsPrivate,
IsPaid = dto.IsPaid,
IsPendingHost = dto.IsPendingHost,
ProductId = dto.ProductId,
ProductVersionId = dto.ProductVersionId,
TicketTypeId = dto.TicketTypeId,
TicketPriorityId = dto.TicketPriorityId,
TicketStateId = dto.TicketStateId,
Id = dto.Id
}: null)
})
/*
.WhereIf(!string.IsNullOrWhiteSpace(input.ProductNameFilter), e => e.ProductName.ToLower() == input.ProductNameFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.ProductVersionNameFilter), e => e.ProductVersionName.ToLower() == input.ProductVersionNameFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.TicketTypeNameFilter), e => e.TicketType.Name.ToLower() == input.TicketTypeNameFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.TicketPriorityNameFilter), e => e.TicketPriority.Name.ToLower() == input.TicketPriorityNameFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.TicketStateNameFilter), e => e.TicketState.Name.ToLower() == input.TicketStateNameFilter.ToLower().Trim())
.WhereIf(!string.IsNullOrWhiteSpace(input.TenantNameFilter), e => e.Tenant.TenancyName.ToLower() == input.TenantNameFilter.ToLower().Trim())
*/
.OrderBy(input.Sorting ?? "ticket.id asc")
.PageBy(input); // equal to .Skip(0).Take(10)
Without Select() it works fine even with OrderBy() . With Select() it works fine only without OrderBy(). Weird...
I looked at the repro code added here. It is using a lot of external libraries like aspnetboilerplate/EPPlus. ASP.NET Core does not support .NET Framework in 3.0 while EPPlus does not support .netcore and is not maintained anymore. There is no way to upgrade the repro code to test out what happens in EF Core 3.1.
Since there are a lot of external library which could be contributing to the issue, I am closing this issue as not needed. If you manage to upgrade to EF Core 3.1 and still run into issue, please file a new issue with detailed repro which does not use external libraries.
