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

Allow inline primitive collections with parameters, translating to VALUES #30732

Closed
Tracked by #30731
roji opened this issue Apr 20, 2023 · 0 comments · Fixed by #31046
Closed
Tracked by #30731

Allow inline primitive collections with parameters, translating to VALUES #30732

roji opened this issue Apr 20, 2023 · 0 comments · Fixed by #31046
Assignees
Labels
area-perf area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented Apr 20, 2023

In the following query:

public virtual Task Inline_collection_Contains_with_all_parameters(bool async)
{
    var (i, j) = (2, 999);

    return AssertQuery(
        async,
        ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => new[] { i, j }.Contains(c.Id)),
        entryCount: 1);
}

ParameterExtractingEV client-evaluates the NewArrayExpression (since it contains no server-correlated data), and transforms the entire thing into a single parameter. This causes us to send the following SQL on SQL Server:

@__p_0='[2,999]' (Size = 4000)

SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[String], [p].[Strings]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE EXISTS (
    SELECT 1
    FROM OpenJson(@__p_0) AS [p0] -- Not good
    WHERE CAST([p0].[value] AS int) = [p].[Id])

Ideally, this LINQ query would be translated to an IN expression with two parameters instead:

WHERE [p.Id] IN (@i, @j)

This would reproduce the constant array in the original LINQ query, and would give the database the opportunity to optimize for the fact that there are two parameters; it would very likely run faster than the generic OpenJson version, which has the same SQL regardless of the number of parameters.

The main problem here is the change to refrain client-evaluating the NewArrayExpression even though it contains no database-correlated things. Allowing arbitrary expressions in the inline expression (not just constants and parameters) is tracked by #30734.

@roji roji changed the title Consider not client-evaluating NewArrayExpression in parameter extractor Consider not client-evaluating NewArrayInitExpression in parameter extractor Apr 20, 2023
@roji roji changed the title Consider not client-evaluating NewArrayInitExpression in parameter extractor Consider not client-evaluating NewArrayExpression in parameter extractor Apr 20, 2023
roji added a commit to roji/efcore that referenced this issue Apr 27, 2023
roji added a commit to roji/efcore that referenced this issue Apr 27, 2023
roji added a commit to roji/efcore that referenced this issue Apr 27, 2023
roji added a commit to roji/efcore that referenced this issue Apr 28, 2023
roji added a commit to roji/efcore that referenced this issue Apr 28, 2023
roji added a commit to roji/efcore that referenced this issue Apr 28, 2023
roji added a commit to roji/efcore that referenced this issue Apr 29, 2023
@roji roji changed the title Consider not client-evaluating NewArrayExpression in parameter extractor Allow inline primitive collections with parameters, translating to VALUES May 2, 2023
roji added a commit to roji/efcore that referenced this issue May 3, 2023
roji added a commit to roji/efcore that referenced this issue May 3, 2023
roji added a commit to roji/efcore that referenced this issue May 8, 2023
roji added a commit to roji/efcore that referenced this issue May 8, 2023
roji added a commit to roji/efcore that referenced this issue May 8, 2023
@ajcvickers ajcvickers added this to the 8.0.0 milestone May 16, 2023
roji added a commit to roji/efcore that referenced this issue May 31, 2023
roji added a commit to roji/efcore that referenced this issue May 31, 2023
roji added a commit to roji/efcore that referenced this issue Jun 4, 2023
roji added a commit to roji/efcore that referenced this issue Jun 6, 2023
roji added a commit to roji/efcore that referenced this issue Jun 6, 2023
roji added a commit to roji/efcore that referenced this issue Jun 6, 2023
roji added a commit to roji/efcore that referenced this issue Jun 6, 2023
roji added a commit to roji/efcore that referenced this issue Jun 7, 2023
roji added a commit to roji/efcore that referenced this issue Jun 13, 2023
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 22, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0, 8.0.0-preview6 Jun 22, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-preview6, 8.0.0 Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-perf area-query 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
2 participants