Skip to content

[1.0.1] Fix to #5738 - Query Perf: Selecting entire table for navigation properties in projections - #6022

Merged
maumar merged 1 commit into
hotfix-1.0.1from
fix5738_take2
Jul 8, 2016
Merged

maumar merged 1 commit into
hotfix-1.0.1from
fix5738_take2

Conversation

@maumar

@maumar maumar commented Jul 7, 2016

Copy link
Copy Markdown
Contributor

Fix is to allow more queries to be translated to SQL, specifically subqueries that are correlated with outer query, e.g.

customers.Select(c => c.Orders.FirstOrDefault())

would be translated to:

customers.Select(c => orders.Where(o => o.CustomerId == c.Id).FirstOrDefault())

This would previously produce outer query to fetch all the customers and for each customer another query, fetching ALL the orders each time filtering out the orders for each customer and returning only the first one on the client (because we didn't know how to handle o.CustomerId in this case)

With this change we will convert o.CustomerId into a SQL parameter, whose value is set for each iteration of the outer query - this can be easily translated to SQL and produce much more efficient queries:

SELECT [t].[CustomerID]
FROM [Customers] AS [c]

@_outer_CustomerID: ALFKI (Size = 450)

SELECT TOP(1) [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE @_outer_CustomerID = [o].[CustomerID]

We achieve this by introducing a new query method that creates additional parameters before enumerating it's source, and then removing those parameters after all elements have been enumerated or the enumerator has been disposed.
This way we make sure that the additional parameters only exist in the scope that the inner query is executed.

This also forces some changes in the CommandCache - we used QueryContext.ParameterValues as cache key, but since now the parameter values are modified during query execution we need to rely on cloning them at and used the cloned values as the cache key.

@maumar

maumar commented Jul 7, 2016

Copy link
Copy Markdown
Contributor Author

@anpete

@maumar maumar changed the title Fix to #5738 - Query Perf: Selecting entire table for navigation properties in projections [1.0.1] Fix to #5738 - Query Perf: Selecting entire table for navigation properties in projections Jul 7, 2016
var result = await _innerEnumerator.MoveNext(cancellationToken);
if (!result)
{
RemoveParameters();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be safe to just rely on Dispose to do this.

@anpete

anpete commented Jul 7, 2016

Copy link
Copy Markdown
Contributor

Looking good. :shipit: after feedback and R#

…bqueries that are correlated with outer query, e.g.

customers.Select(c => c.Orders.FirstOrDefault())

would be translated to:

customers.Select(c => orders.Where(o => o.CustomerId == c.Id).FirstOrDefault())

This would previously produce outer query to fetch all the customers and for each customer another query, fetching ALL the orders each time filtering out the orders for each customer and returning only the first one on the client (because we didn't know how to handle o.CustomerId in this case)

With this change we will convert o.CustomerId into a SQL parameter, whose value is set for each iteration of the outer query - this can be easily translated to SQL and produce much more efficient queries:

SELECT [t].[CustomerID]
FROM [Customers] AS [c]

@_outer_CustomerID: ALFKI (Size = 450)

SELECT TOP(1) [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE @_outer_CustomerID = [o].[CustomerID]

We achieve this by introducing a new query method that creates additional parameters before enumerating it's source, and then removing those parameters after all elements have been enumerated or the enumerator has been disposed.
This way we make sure that the additional parameters only exist in the scope that the inner query is executed.

This also forces some changes in the CommandCache - we used QueryContext.ParameterValues as cache key, but since now the parameter values are modified during query execution we need to rely on cloning them at and used the cloned values as the cache key.
CR: Andrew
@rowanmiller

Copy link
Copy Markdown
Contributor

We decided this is too high risk for 1.0.1, so just push it into dev

@maumar
maumar merged commit 05b24c3 into hotfix-1.0.1 Jul 8, 2016
@maumar
maumar deleted the fix5738_take2 branch July 19, 2016 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants