Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N+1 queries when returning entity but not when projecting properties of entity #11517

Closed
ShamsulAmry opened this issue Apr 1, 2018 · 2 comments

Comments

@ShamsulAmry
Copy link
Contributor

Issue as per subject. Please see comment in code fragment below.

public class TestContext : DbContext
{
    RouteMaster { get; set; }

    public async Task TestAsync()
    {
        var badTestQuery =
            from rm in RouteMaster
            let schedules =
                from rs in rm.Schedules
                where rs.CutOffTime >= new TimeSpan(9, 0, 0) && rs.CutOffTime <= new TimeSpan(17, 0, 0)
                select rs
            select new {
                // This query will return full entity
                rm,
                HasActiveSchedule = schedules.Any()
            };

        // This will result in N+1 queries. One for RouteMaster and N for the RouteMaster's Schedules
        var badTestResult = await badTestQuery.ToArrayAsync().ConfigureAwait(false);

        var goodTestQuery =
            from rm in RouteMaster
            let schedules =
                from rs in rm.Schedules
                where rs.CutOffTime >= new TimeSpan(9, 0, 0) && rs.CutOffTime <= new TimeSpan(17, 0, 0)
                select rs
            select new {
                // This query will project all properties of the entity, same as it would be in the above bad query
                rm.Id,
                rm.RouteNumber,
                HasActiveSchedule = schedules.Any()
            };

        // This will result in one select statement with the RouteMaster's Schedules as sql subquery
        var goodTestResult = await goodTestQuery.ToArrayAsync().ConfigureAwait(false);  
    }

    class RouteMasterRecord
    {
        public Guid Id { get; private set; }
        public string RouteNumber { get; private set; }
        public IReadOnlyCollection<RouteScheduleRecord> Schedules { get; private set; }
    }

    class RouteScheduleRecord
    {
        public Guid Id {get; private set; }
        public TimeSpan CutOffTime { get; private set; }
    }
}

Further technical details

EF Core version: 2.1.0-preview1-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.5.7

@divega
Copy link
Contributor

divega commented Apr 2, 2018

@smitpatel @maumar please find the duplicate.

@smitpatel
Copy link
Member

Duplicate of #11186

@smitpatel smitpatel marked this as a duplicate of #11186 Apr 2, 2018
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants