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: Lift subquery with nested orderby #8054

Closed
anpete opened this issue Apr 2, 2017 · 9 comments
Closed

Query: Lift subquery with nested orderby #8054

anpete opened this issue Apr 2, 2017 · 9 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

@anpete
Copy link
Contributor

anpete commented Apr 2, 2017

The following query:

from c1_Orders in context.Orders
join _c1 in
   (from c1 in
	(from c in context.Customers
		orderby c.CustomerID
		select c)
	.Take(2)
	from c2 in context.Customers
	select new { CustomerID = EF.Property<string>(c1, "CustomerID") })
   .Distinct()
on EF.Property<string>(c1_Orders, "CustomerID") equals _c1.CustomerID
orderby _c1.CustomerID
select c1_Orders

produces

@__p_0: 2

SELECT DISTINCT [t0].[CustomerID]
FROM (
    SELECT TOP(@__p_0) [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
    FROM [Customers] AS [c0]
    ORDER BY [c0].[CustomerID]
) AS [t0]
CROSS JOIN [Customers] AS [c20]
ORDER BY [t0].[CustomerID]

SELECT [c1_Orders].[OrderID], [c1_Orders].[CustomerID], [c1_Orders].[EmployeeID], [c1_Orders].[OrderDate]
FROM [Orders] AS [c1_Orders]

I think this is because of the orderby lifting happening in SE.PushDownSubquery. This query represents some Include queries that are worse with the new compiler

@anpete
Copy link
Contributor Author

anpete commented Apr 2, 2017

@smitpatel @maumar Any ideas?

Another thing I noticed is that 'join _c1 in..' is being marked for materialization.

anpete added a commit that referenced this issue Apr 2, 2017
@smitpatel
Copy link
Contributor

What is the condition which is blocking flattening of join?

In Sql, join clause is not being materialized but inner Customer is which is weird.

@anpete
Copy link
Contributor Author

anpete commented Apr 3, 2017

@smitpatel I think it is the presence of OrderBy without skip/take. The OrderBy is lifted to the outer table expression during Subquery push-down.

@smitpatel
Copy link
Contributor

If we remove the code which is lifting order by outside in PushDownSubquery, does this query work fine?

@anpete
Copy link
Contributor Author

anpete commented Apr 3, 2017

Yes! But others don't 😬

@smitpatel
Copy link
Contributor

@anpete - Is this blocking issue for you? The side effects so far I have found

  • Join ignores ordering for join clause. Causing ordering difference between l2o/sql queries
  • Unnecessary pushdown happening when we don't lift joins.
  • There is a blocking bug in ProjectionShaper.

@anpete
Copy link
Contributor Author

anpete commented Apr 6, 2017

@smitpatel Not blocking, just regresses certain include queries. Do we know how many other queries may be improved if we fix this?

@smitpatel
Copy link
Contributor

I worked out to remove copying order by for all push down queries to only limit/offset/group join (include works because query model already lifts it).
There is only 1 failure left which is group join which requires ordering added by us and has inner ordering too. RowNumberPagingExpressionVisitor is having trouble dealing with it.

@smitpatel
Copy link
Contributor

Also, query in first post fails at runtime due to bug in projection shaper. I did not see any other query being lifted anywhere though.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 10, 2017
@ajcvickers ajcvickers changed the title Subquery with nested orderby is not lifted Query: Subquery with nested orderby is not lifted May 9, 2017
@smitpatel smitpatel changed the title Query: Subquery with nested orderby is not lifted Query: Lift subquery with nested orderby May 9, 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

4 participants