Skip to content

Commit

Permalink
Minor improvement of SqlServerParameterBasedSqlProcessor.Optimize (#…
Browse files Browse the repository at this point in the history
…33758)

* Collapse `Take 0` before optimizing the expression
This opens up some additional query optimization opportunities.
  • Loading branch information
ranma42 authored Jun 4, 2024
1 parent ed6213d commit abfc584
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public override Expression Optimize(
IReadOnlyDictionary<string, object?> parametersValues,
out bool canCache)
{
var optimizedQueryExpression = base.Optimize(queryExpression, parametersValues, out canCache);
var optimizedQueryExpression = new SkipTakeCollapsingExpressionVisitor(Dependencies.SqlExpressionFactory)
.Process(queryExpression, parametersValues, out var canCache2);

optimizedQueryExpression = new SkipTakeCollapsingExpressionVisitor(Dependencies.SqlExpressionFactory)
.Process(optimizedQueryExpression, parametersValues, out var canCache2);
optimizedQueryExpression = base.Optimize(optimizedQueryExpression, parametersValues, out canCache);

canCache &= canCache2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5925,13 +5925,7 @@ public override async Task Skip_0_Take_0_works_when_constant(bool async)

AssertSql(
"""
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Orders] AS [o]
WHERE 0 = 1) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
SELECT CAST(0 AS bit)
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'F%'
ORDER BY [c].[CustomerID]
Expand All @@ -5944,13 +5938,7 @@ public override async Task Skip_1_Take_0_works_when_constant(bool async)

AssertSql(
"""
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Orders] AS [o]
WHERE 0 = 1) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
SELECT CAST(0 AS bit)
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'F%'
ORDER BY [c].[CustomerID]
Expand Down

0 comments on commit abfc584

Please sign in to comment.