-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: TPC prune unused concrete tables and columns #27993
Conversation
SELECT [k].[Species], [k].[CountryId], [k].[Name], [k].[EagleId], [k].[IsFlightless], NULL AS [Group], [k].[FoundOn], N'Kiwi' AS [Discriminator] | ||
FROM [Kiwi] AS [k] | ||
) AS [t] | ||
WHERE [t].[CountryId] = 1 AND [t].[Discriminator] = N'Kiwi'"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Table pruning example
@@ -77,10 +74,10 @@ public override async Task Can_use_is_kiwi_in_projection(bool async) | |||
ELSE CAST(0 AS bit) | |||
END | |||
FROM ( | |||
SELECT [e].[Species], [e].[CountryId], [e].[Name], [e].[EagleId], [e].[IsFlightless], [e].[Group], NULL AS [FoundOn], N'Eagle' AS [Discriminator] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Column pruning example
@@ -1350,6 +1328,59 @@ public void ApplyPredicate(SqlExpression sqlExpression) | |||
sqlExpression = PushdownIntoSubqueryInternal().Remap(sqlExpression); | |||
} | |||
|
|||
if ((sqlExpression is SqlBinaryExpression { OperatorType: ExpressionType.Equal } | |||
|| sqlExpression is InExpression { Subquery: null, IsNegated: false }) | |||
&& _groupBy.Count == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only condition in Select before swallowing predicate and pruning table.
@maumar to review.
Limit/Offset are deal with above itself. If they are present, pushdown will happen and we won't find the Tpc table anyway.
Other predicates are fine since they do AND so it will work.
GroupBy is not allowed, also implies no HAVING
DISTINCT is ok because it applies after filtering.
Other tables in SelectExpression doesn't affect the base as predicate applies to whole result. For TPC in joined table, the predicate would be specified before the join (that scenario works already). If predicate is applied to result of join then it becomes complicated to track down TPC because it would be inside subquery in joined table. User can always do filtering before.
Ordering is applied after filtering so that is also fine (unless limit/offset which is already handled).
Do we need anymore condition (certain thing on Select) which makes this optimization incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be useful to add this comment in the code
- Add TpcTablesExpression which holds all union all tables so we can prune unused subqueries - Also allow pruning of unused column inside each subquery - Assign unique aliases in inner subqueries across whole SelectExpression - Apply predicate on discriminator column directly to subqueries by removing unnecessary subqueries Resolves #27967 Resolves #27957
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another reviewer would be good
UNION ALL | ||
SELECT [o0].[Nickname], [o0].[SquadId], [o0].[AssignedCityName], [o0].[CityOfBirthName], [o0].[FullName], [o0].[HasSoulPatch], [o0].[LeaderNickname], [o0].[LeaderSquadId], [o0].[Rank], N'Officer' AS [Discriminator] | ||
FROM [Officers] AS [o0] | ||
SELECT [t2].[Nickname], [t2].[Rank], [t2].[SquadId], [t2].[FullName], [t2].[LeaderNickname], [t2].[LeaderSquadId] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could lift this case also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #28009
FROM [Officers] AS [o4] | ||
) AS [t7] ON [s].[Id] = [t7].[SquadId] | ||
SELECT [t10].[Nickname], [t10].[HasSoulPatch], [t10].[SquadId] | ||
FROM ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
UNION ALL | ||
SELECT [o0].[Nickname], [o0].[SquadId], [o0].[AssignedCityName], [o0].[CityOfBirthName], [o0].[FullName], [o0].[HasSoulPatch], [o0].[LeaderNickname], [o0].[LeaderSquadId], [o0].[Rank], N'Officer' AS [Discriminator] | ||
FROM [Officers] AS [o0] | ||
SELECT [t3].[FullName], [t3].[Nickname], [t3].[SquadId], [t3].[LeaderNickname], [t3].[LeaderSquadId] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and here
Resolves #27967
Resolves #27957