-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Optimize certain types of GroupJoin/SelectMany queries
In some circumstances, a GroupJoin/SelectMany pattern (with or without DefaultIfEmpty) can be optimized by shifting body clauses from the AdditionalFromClause into the GroupJoinClause. These changes implement that optimization.
- Loading branch information
Showing
3 changed files
with
141 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
99 changes: 99 additions & 0 deletions
99
...Core/Query/ExpressionVisitors/Internal/AdditionalFromClauseOptimizingQueryModelVisitor.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query.Internal; | ||
using Remotion.Linq; | ||
using Remotion.Linq.Clauses; | ||
using Remotion.Linq.Clauses.Expressions; | ||
using Remotion.Linq.Clauses.ExpressionVisitors; | ||
using Remotion.Linq.Clauses.ResultOperators; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class AdditionalFromClauseOptimizingQueryModelVisitor : QueryModelVisitorBase | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public override void VisitQueryModel([NotNull] QueryModel queryModel) | ||
{ | ||
queryModel.TransformExpressions(new TransformingQueryModelExpressionVisitor<AdditionalFromClauseOptimizingQueryModelVisitor>(this).Visit); | ||
|
||
base.VisitQueryModel(queryModel); | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public override void VisitAdditionalFromClause( | ||
[NotNull] AdditionalFromClause fromClause, | ||
[NotNull] QueryModel queryModel, | ||
int index) | ||
{ | ||
if (fromClause.FromExpression is SubQueryExpression subQueryExpression | ||
&& subQueryExpression.QueryModel.MainFromClause.FromExpression is QuerySourceReferenceExpression qsre | ||
&& subQueryExpression.QueryModel.SelectClause.Selector is QuerySourceReferenceExpression | ||
&& qsre.ReferencedQuerySource is GroupJoinClause groupJoinClause | ||
&& queryModel.CountQuerySourceReferences(groupJoinClause) == 1 | ||
&& subQueryExpression.QueryModel.BodyClauses.Any() | ||
&& subQueryExpression.QueryModel.BodyClauses.All(c => c is WhereClause)) | ||
{ | ||
if (groupJoinClause.JoinClause.InnerSequence is SubQueryExpression innerSequenceSubQuery) | ||
{ | ||
if (!innerSequenceSubQuery.QueryModel.ResultOperators.Any(r => r is TakeResultOperator || r is SkipResultOperator)) | ||
{ | ||
ShiftWhereClauses(subQueryExpression.QueryModel, innerSequenceSubQuery.QueryModel); | ||
} | ||
} | ||
else | ||
{ | ||
var newMainFromClause = new MainFromClause( | ||
groupJoinClause.JoinClause.ItemName, | ||
groupJoinClause.JoinClause.ItemType, | ||
groupJoinClause.JoinClause.InnerSequence); | ||
|
||
var newSelectClause = new SelectClause( | ||
new QuerySourceReferenceExpression(newMainFromClause)); | ||
|
||
var newSubQueryModel = new QueryModel(newMainFromClause, newSelectClause); | ||
|
||
ShiftWhereClauses(subQueryExpression.QueryModel, newSubQueryModel); | ||
|
||
var newSubQueryExpression = new SubQueryExpression(newSubQueryModel); | ||
|
||
groupJoinClause.JoinClause.InnerSequence = newSubQueryExpression; | ||
} | ||
} | ||
} | ||
|
||
private void ShiftWhereClauses(QueryModel oldQueryModel, QueryModel newQueryModel) | ||
{ | ||
var querySourceMapping = new QuerySourceMapping(); | ||
|
||
querySourceMapping.AddMapping( | ||
oldQueryModel.MainFromClause, | ||
new QuerySourceReferenceExpression(newQueryModel.MainFromClause)); | ||
|
||
foreach (var whereClause in oldQueryModel.BodyClauses.Cast<WhereClause>().ToArray()) | ||
{ | ||
whereClause.Predicate | ||
= ReferenceReplacingExpressionVisitor.ReplaceClauseReferences( | ||
whereClause.Predicate, | ||
querySourceMapping, | ||
throwOnUnmappedReferences: false); | ||
|
||
oldQueryModel.BodyClauses.Remove(whereClause); | ||
newQueryModel.BodyClauses.Add(whereClause); | ||
} | ||
} | ||
} | ||
} |
This file contains 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