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

Query: Flatten out subquery models at query optimizer #7613

Closed
smitpatel opened this issue Feb 15, 2017 · 3 comments
Closed

Query: Flatten out subquery models at query optimizer #7613

smitpatel opened this issue Feb 15, 2017 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@smitpatel
Copy link
Member

  '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.
@BladeWise
Copy link
Contributor

BladeWise commented Feb 21, 2017

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

@smitpatel
Copy link
Member Author

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

@smitpatel smitpatel assigned maumar and unassigned smitpatel Apr 4, 2017
@maumar
Copy link
Contributor

maumar commented Apr 5, 2017

fixed in f022d66

@maumar maumar closed this as completed Apr 5, 2017
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 5, 2017
@divega divega modified the milestone: 2.0.0-preview1 May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

6 participants