Skip to content

Query: in case of joins, we shouldn't be materializing Inner if the qsre itself is not present in final projection #10210

Description

@maumar

Query:

from g in ctx.Gears
join inner in (
     from g2 in ctx.Gears
     select new { g2.Nickname }
     ) on g.Nickname equals inner.Nickname
select g;

produces the following query plan:

(QueryContext queryContext) => IEnumerable<Gear> _InterceptExceptions(
    source: IEnumerable<Gear> _ShapedQuery(
        queryContext: queryContext, 
        shaperCommandContext: SelectExpression: 
            SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOrBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank], [t].[Nickname]
            FROM [Gears] AS [g]
            INNER JOIN (
                SELECT [g2].[Nickname]
                FROM [Gears] AS [g2]
                WHERE [g2].[Discriminator] IN (N'Officer', N'Gear')
            ) AS [t] ON [g].[Nickname] = [t].[Nickname]
            WHERE [g].[Discriminator] IN (N'Officer', N'Gear'), 
        shaper: (BufferedOffsetEntityShaper<Gear>
        materializer: (TransparentIdentifier<Gear, <>f__AnonymousType227<string>> t0) => t0.Outer)), 
    contextType: TestModels.GearsOfWarModel.GearsOfWarContext, 
    logger: DiagnosticsLogger<Query>, 
    queryContext: queryContext)

We actually don't need to materialize inner, so the materializer could be simplified by stripping the transparent identifier.

Another case that could also be improved, is when element of the inner is present in the final projection, but the inner qsre is not. In that case, we also don't need to materialize the inner and we can directly bind to the value buffer instead:

from g in ctx.Gears
join inner in (
     from g2 in ctx.Gears
     select new { g2.Nickname }
     ) on g.Nickname equals inner.Nickname
select new { g, inner.Nickname };

Activity

  1. smitpatel commented on Nov 2, 2017

    @smitpatel
    Contributor

    Also related #9892

  2. added this to the 2.1.0 milestone on Nov 6, 2017
  3. modified the milestones: , 2.1.0 on Jan 17, 2018
  4. modified the milestones: 2.1.0, Backlog on Jan 26, 2018
  5. smitpatel commented on Apr 11, 2018

    @smitpatel
    Contributor
            [ConditionalFact]
            public virtual void Anonymous_projection_AsNoTracking_Min()
            {
                AssertQueryScalar<Order>(
                    os => os.Select(o => new { A = o.CustomerID, B = o.OrderDate })
                        .AsNoTracking() // Just to cause a subquery
                        .Select(e => e.B));
            }

    This causes the selector to be evaluated on client. With aggregate operator like Min/Max causes really bad query. Eliminating anonymous types would be helpful in such cases.

  6. modified the milestones: Backlog, on Sep 4, 2019
  7. maumar commented on Sep 11, 2019

    @maumar
    ContributorAuthor

    verified this issue has been fixed in 3.0

  8. modified the milestones: , 3.0.0 on Sep 11, 2019
  9. added 2 commits that reference this issue on Sep 12, 2019
    6443c98
    36a7bdf
  10. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions