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

Optimize DISTINCT over singleton collections #34702

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ public void ApplyDistinct()
throw new InvalidOperationException(RelationalStrings.DistinctOnCollectionNotSupported);
}

if (Limit is SqlConstantExpression { Value: 1 })
{
return;
}

if (Limit != null
|| Offset != null)
{
Expand Down Expand Up @@ -1915,6 +1920,11 @@ public void ApplyLimit(SqlExpression sqlExpression)
}

Limit = sqlExpression;

if (Offset is null && Limit is SqlConstantExpression { Value: 1 })
{
IsDistinct = false;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where l1.Id < 3
SELECT (
SELECT TOP(1) [l1].[Name]
FROM (
SELECT DISTINCT TOP(1) [l0].[Id], [l0].[Level2_Optional_Id], [l0].[Level2_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse3Id], [l0].[OneToMany_Optional_Self_Inverse3Id], [l0].[OneToMany_Required_Inverse3Id], [l0].[OneToMany_Required_Self_Inverse3Id], [l0].[OneToOne_Optional_PK_Inverse3Id], [l0].[OneToOne_Optional_Self3Id]
SELECT TOP(1) [l0].[Id], [l0].[Name]
FROM [LevelThree] AS [l0]
) AS [l1]
ORDER BY [l1].[Id])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where l1.Id < 3
SELECT (
SELECT TOP(1) [l1].[Name]
FROM (
SELECT DISTINCT TOP(1) [l0].[Id], [l0].[Level2_Optional_Id], [l0].[Level2_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse3Id], [l0].[OneToMany_Optional_Self_Inverse3Id], [l0].[OneToMany_Required_Inverse3Id], [l0].[OneToMany_Required_Self_Inverse3Id], [l0].[OneToOne_Optional_PK_Inverse3Id], [l0].[OneToOne_Optional_Self3Id]
SELECT TOP(1) [l0].[Id], [l0].[Name]
FROM [LevelThree] AS [l0]
) AS [l1]
ORDER BY [l1].[Id])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where l1.Id < 3
SELECT (
SELECT TOP(1) [s].[Level3_Name]
FROM (
SELECT DISTINCT TOP(1) [l4].[Id], [l4].[Level2_Optional_Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Optional_Inverse3Id], [l4].[OneToMany_Required_Inverse3Id], [l4].[OneToOne_Optional_PK_Inverse3Id]
SELECT TOP(1) [l4].[Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Required_Inverse3Id]
FROM [Level1] AS [l0]
LEFT JOIN (
SELECT [l1].[Id], [l1].[OneToOne_Required_PK_Date], [l1].[Level1_Required_Id], [l1].[OneToMany_Required_Inverse2Id]
Expand All @@ -97,7 +97,7 @@ WHERE [l1].[OneToOne_Required_PK_Date] IS NOT NULL AND [l1].[Level1_Required_Id]
WHEN [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] IS NOT NULL AND [l2].[OneToMany_Required_Inverse2Id] IS NOT NULL THEN [l2].[Id]
END
LEFT JOIN (
SELECT [l3].[Id], [l3].[Level2_Optional_Id], [l3].[Level2_Required_Id], [l3].[Level3_Name], [l3].[OneToMany_Optional_Inverse3Id], [l3].[OneToMany_Required_Inverse3Id], [l3].[OneToOne_Optional_PK_Inverse3Id]
SELECT [l3].[Id], [l3].[Level2_Required_Id], [l3].[Level3_Name], [l3].[OneToMany_Required_Inverse3Id]
FROM [Level1] AS [l3]
WHERE [l3].[Level2_Required_Id] IS NOT NULL AND [l3].[OneToMany_Required_Inverse3Id] IS NOT NULL
) AS [l4] ON CASE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where l1.Id < 3
SELECT (
SELECT TOP(1) [s].[Level3_Name]
FROM (
SELECT DISTINCT TOP(1) [l4].[Id], [l4].[Level2_Optional_Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Optional_Inverse3Id], [l4].[OneToMany_Required_Inverse3Id], [l4].[OneToOne_Optional_PK_Inverse3Id]
SELECT TOP(1) [l4].[Id], [l4].[Level2_Required_Id], [l4].[Level3_Name], [l4].[OneToMany_Required_Inverse3Id]
FROM [Level1] AS [l0]
LEFT JOIN (
SELECT [l1].[Id], [l1].[OneToOne_Required_PK_Date], [l1].[Level1_Required_Id], [l1].[OneToMany_Required_Inverse2Id]
Expand All @@ -99,7 +99,7 @@ WHERE [l1].[OneToOne_Required_PK_Date] IS NOT NULL AND [l1].[Level1_Required_Id]
WHEN [l2].[OneToOne_Required_PK_Date] IS NOT NULL AND [l2].[Level1_Required_Id] IS NOT NULL AND [l2].[OneToMany_Required_Inverse2Id] IS NOT NULL THEN [l2].[Id]
END
LEFT JOIN (
SELECT [l3].[Id], [l3].[Level2_Optional_Id], [l3].[Level2_Required_Id], [l3].[Level3_Name], [l3].[OneToMany_Optional_Inverse3Id], [l3].[OneToMany_Required_Inverse3Id], [l3].[OneToOne_Optional_PK_Inverse3Id]
SELECT [l3].[Id], [l3].[Level2_Required_Id], [l3].[Level3_Name], [l3].[OneToMany_Required_Inverse3Id]
FROM [Level1] AS [l3]
WHERE [l3].[Level2_Required_Id] IS NOT NULL AND [l3].[OneToMany_Required_Inverse3Id] IS NOT NULL
) AS [l4] ON CASE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ public override async Task Where_subquery_distinct_singleordefault_boolean2(bool
SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[Rank]
FROM [Gears] AS [g]
WHERE [g].[HasSoulPatch] = CAST(1 AS bit) AND COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit)) = CAST(1 AS bit)
ORDER BY [g].[Nickname]
Expand Down Expand Up @@ -5838,7 +5838,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean2(boo
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit))
FROM [Gears] AS [g]
Expand Down Expand Up @@ -5889,7 +5889,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean_empt
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] = N'BFG'), CAST(0 AS bit))
FROM [Gears] AS [g]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ public override async Task Project_single_element_from_collection_with_OrderBy_D
AssertSql(
"""
SELECT (
SELECT DISTINCT TOP(1) [o].[CustomerID]
SELECT TOP(1) [o].[CustomerID]
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID])
FROM [Customers] AS [c]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,7 @@ UNION ALL
FROM [Officers] AS [o]
) AS [u]
WHERE [u].[HasSoulPatch] = CAST(1 AS bit) AND COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [u].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit)) = CAST(1 AS bit)
ORDER BY [u].[Nickname]
Expand Down Expand Up @@ -7957,7 +7957,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean2(boo
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [u].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit))
FROM (
Expand Down Expand Up @@ -8026,7 +8026,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean_empt
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [u].[FullName] = [w].[OwnerFullName] AND [w].[Name] = N'BFG'), CAST(0 AS bit))
FROM (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ END AS [Discriminator]
FROM [Gears] AS [g]
LEFT JOIN [Officers] AS [o] ON [g].[Nickname] = [o].[Nickname] AND [g].[SquadId] = [o].[SquadId]
WHERE [g].[HasSoulPatch] = CAST(1 AS bit) AND COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit)) = CAST(1 AS bit)
ORDER BY [g].[Nickname]
Expand Down Expand Up @@ -6678,7 +6678,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean2(boo
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit))
FROM [Gears] AS [g]
Expand Down Expand Up @@ -6729,7 +6729,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean_empt
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] = N'BFG'), CAST(0 AS bit))
FROM [Gears] AS [g]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean2(boo
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit))
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
Expand Down Expand Up @@ -2395,7 +2395,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean_empt
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] = N'BFG'), CAST(0 AS bit))
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
Expand Down Expand Up @@ -6607,7 +6607,7 @@ public override async Task Where_subquery_distinct_singleordefault_boolean2(bool
SELECT [g].[Nickname], [g].[SquadId], [g].[AssignedCityName], [g].[CityOfBirthName], [g].[Discriminator], [g].[FullName], [g].[HasSoulPatch], [g].[LeaderNickname], [g].[LeaderSquadId], [g].[PeriodEnd], [g].[PeriodStart], [g].[Rank]
FROM [Gears] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [g]
WHERE [g].[HasSoulPatch] = CAST(1 AS bit) AND COALESCE((
SELECT DISTINCT TOP(1) [w].[IsAutomatic]
SELECT TOP(1) [w].[IsAutomatic]
FROM [Weapons] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [w]
WHERE [g].[FullName] = [w].[OwnerFullName] AND [w].[Name] LIKE N'%Lancer%'), CAST(0 AS bit)) = CAST(1 AS bit)
ORDER BY [g].[Nickname]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2335,7 +2335,7 @@ public override async Task Where_subquery_distinct_singleordefault_boolean2(bool
SELECT "g"."Nickname", "g"."SquadId", "g"."AssignedCityName", "g"."CityOfBirthName", "g"."Discriminator", "g"."FullName", "g"."HasSoulPatch", "g"."LeaderNickname", "g"."LeaderSquadId", "g"."Rank"
FROM "Gears" AS "g"
WHERE "g"."HasSoulPatch" AND COALESCE((
SELECT DISTINCT "w"."IsAutomatic"
SELECT "w"."IsAutomatic"
FROM "Weapons" AS "w"
WHERE "g"."FullName" = "w"."OwnerFullName" AND instr("w"."Name", 'Lancer') > 0
LIMIT 1), 0)
Expand Down Expand Up @@ -4852,7 +4852,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean2(boo
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT "w"."IsAutomatic"
SELECT "w"."IsAutomatic"
FROM "Weapons" AS "w"
WHERE "g"."FullName" = "w"."OwnerFullName" AND instr("w"."Name", 'Lancer') > 0
LIMIT 1), 0)
Expand Down Expand Up @@ -7320,7 +7320,7 @@ public override async Task Select_subquery_distinct_singleordefault_boolean_empt
AssertSql(
"""
SELECT COALESCE((
SELECT DISTINCT "w"."IsAutomatic"
SELECT "w"."IsAutomatic"
FROM "Weapons" AS "w"
WHERE "g"."FullName" = "w"."OwnerFullName" AND "w"."Name" = 'BFG'
LIMIT 1), 0)
Expand Down