Skip to content

Query: Flatten out subquery models at query optimizer #7613

Closed

Description

@smitpatel
  'from Customer c in 
      from Customer c in DbSet<Customer>
      where c.City == "Seattle"
      order by c.CustomerID asc
      select c
  where c.CustomerID == "ALFKI"
  select c'

Query model like above can be optimized by merging the bodyclauses.
Points to consider:

  • Inner selector is selecting query source directly.
  • No multiple from clauses inside subquery
  • Presently ReLinq blocks lifting of subquery if there is orderby clause due to possible ambiguity in ordering. We can still lift if only one (inner or outer) contains OrderBy clause.
  • outer selector can be anything.
  • No result operator on subquery.

Activity

  1. added this to the 2.0.0 milestone on Feb 15, 2017
  2. BladeWise commented on Feb 21, 2017

    @BladeWise
    Contributor

    Would this scenario be covered by such optiimization?

    var entities = context.Set<Entity>();
    
    var info = entities.Select(x => new
                                    {
                                       MaxId = entities.Max(x => x.Id),
                                       Count = entities.Count()
                                    })
                       .FirstOrDefault();

    In this case the generated query is

    SELECT TOP 1
         (SELECT MAX(Id) FROM Entities),
         (SELECT COUNT(*) FROM Entities)
    FROM Entities

    while the optimal result would be

    SELECT TOP 1
         MAX(ID),
         COUNT(*)
    FROM Entities
  3. smitpatel commented on Apr 4, 2017

    @smitpatel
    ContributorAuthor

    @maumar - Assigning this to you as this is lifting of order by.

  4. maumar commented on Apr 5, 2017

    @maumar
    Contributor

    fixed in f022d66

  5. modified the milestone: on May 10, 2017
  6. modified the milestones: , 2.0.0 on Oct 15, 2022
  7. 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

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions