Repository navigation
[1.0.1] Fix to #5738 - Query Perf: Selecting entire table for navigation properties in projections - #6022
Merged
Conversation
Contributor
Author
| var result = await _innerEnumerator.MoveNext(cancellationToken); | ||
| if (!result) | ||
| { | ||
| RemoveParameters(); |
Contributor
There was a problem hiding this comment.
It should be safe to just rely on Dispose to do this.
Contributor
|
Looking good. |
…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
Contributor
|
We decided this is too high risk for 1.0.1, so just push it into dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.