You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Join condition that isn't an outer/inner column comparison causes a subquery pushdown #33745
fromfooindbContext.Set<Foo>()
join barindbContext.Set<Bar>() on
new{foo.Id,State="Blah"} equals new{Id=bar.FooId,bar.State} into barGroupJoinfromsubBarinbarGroupJoin.DefaultIfEmpty()selectnew{/* use subBar instead of bar */}
Method-style equijoin
fromfooindbContext.Set<Foo>()frombarindbContext.Set<Bar>().Where(x =>x.FooId==foo.Id&&x.State=="Blah").DefaultIfEmpty()selectnew{/* use bar */}
I generally prefer 2 because it's arguably just as easy to read, doesn't leave a variable that goes unused, etc.
But one thing I notice is that the former style will generate something like
LEFT JOIN mySchema."Bars"AS B ON B."FooId"= F."Id"AND B."State"='Blah'
while the latter generates something like
LEFT JOIN (
SELECT …
FROM mySchema."Bars"AS B
WHERE B."State"='Blah'
) as C ON C."FooId"= F."Id"
I took a look and our current join logic only extracts parts of the Where() predicate to the join condition which are comparisons between outer and inner columns. Anything which does not get extracted causes the inner query to get pushed down into a subquery.
For example, the following:
_=await(fromfooincontext.Set<Foo>()frombarincontext.Set<Bar>().Where(x =>x.FooId==foo.Id&&x.State==foo.State).DefaultIfEmpty()select bar
... gets translated to:
SELECT [b].[Id], [b].[FooId], [b].[State]
FROM [Foos] AS [f]
LEFT JOIN [Bars] AS [b] ON [f].[Id] = [b].[FooId] AND [f].[State] = [b].[State]
But the following (as in the OP):
_=await(fromfooincontext.Set<Foo>()frombarincontext.Set<Bar>().Where(x =>x.FooId==foo.Id&&x.State=="Blah").DefaultIfEmpty()select bar
... gets translated to:
SELECT [b0].[Id], [b0].[FooId], [b0].[State]
FROM [Foos] AS [f]
LEFT JOIN (
SELECT [b].[Id], [b].[FooId], [b].[State]
FROM [Bars] AS [b]
WHERE [b].[State] = N'Blah'
) AS [b0] ON [f].[Id] = [b0].[FooId]
I'll look into the reasons things are implemented this way, and we'll consider relaxing this.
Minimal repro
awaitusingvarcontext=newBlogContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();// All comparisons in the predicate compare outer to inner columns -> results in LEFT JOIN without subquery_=await(fromfooincontext.Set<Foo>()frombarincontext.Set<Bar>().Where(x =>x.FooId==foo.Id&&x.State==foo.State).DefaultIfEmpty()selectbar).ToListAsync();// Predicate contains something that isn't a comparison of outer to inner column -> results in LEFT JOIN with subquery_=await(fromfooincontext.Set<Foo>()frombarincontext.Set<Bar>().Where(x =>x.FooId==foo.Id&&x.State=="Blah").DefaultIfEmpty()selectbar).ToListAsync();publicclassBlogContext:DbContext{publicDbSet<Foo>Foos{get;set;}publicDbSet<Bar>Bars{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}publicclassFoo{publicintId{get;set;}publicstringState{get;set;}}publicclassBar{publicintId{get;set;}publicstringState{get;set;}publicintFooId{get;set;}}
changed the title [-]Question: SQL LEFT JOIN clause varies based on declarative- vs. method-style join[/-][+]Join condition that isn't an outer/inner column comparison causes a subquery pushdown[/+]on Sep 17, 2024
Verify that join conditions can indeed be arbitrary bool-returning expression, across the major databases. For example, we can test something like x.Id > 5 || x.Name.StartsWith("foo) on dbfiddle. If so, we should be able to just always lift the predicate out of the inner query, and make it the join condition, regardless of its contents.
The one exception to this is when the inner subquery predicate affects which rows are returned from the subquery:
fromfooincontext.Set<Foo>()frombarincontext.Set<Bar>().Where(x =>x.FooId==foo.Id).OrderBy(bla).Take(5).DefaultIfEmpty()select bar
If the predicate is lifted out of the subquery, arbitrary 5 rows are returned (unrelated to the principal) and we get wrong results. For this case, the subquery must be left as-is, and the current logic extracting the "join key" is probably OK.
Activity
roji commentedon Sep 17, 2024
Sorry it took so long to get a response on this.
I took a look and our current join logic only extracts parts of the Where() predicate to the join condition which are comparisons between outer and inner columns. Anything which does not get extracted causes the inner query to get pushed down into a subquery.
For example, the following:
... gets translated to:
But the following (as in the OP):
... gets translated to:
I'll look into the reasons things are implemented this way, and we'll consider relaxing this.
Minimal repro
[-]Question: SQL LEFT JOIN clause varies based on declarative- vs. method-style join[/-][+]Join condition that isn't an outer/inner column comparison causes a subquery pushdown[/+]roji commentedon Sep 18, 2024
Next steps:
Verify that join conditions can indeed be arbitrary bool-returning expression, across the major databases. For example, we can test something like
x.Id > 5 || x.Name.StartsWith("foo)on dbfiddle. If so, we should be able to just always lift the predicate out of the inner query, and make it the join condition, regardless of its contents.The one exception to this is when the inner subquery predicate affects which rows are returned from the subquery:
If the predicate is lifted out of the subquery, arbitrary 5 rows are returned (unrelated to the principal) and we get wrong results. For this case, the subquery must be left as-is, and the current logic extracting the "join key" is probably OK.
Fold single-table filter subqueries into joins on SQLite (#38692)
Fold single-table filter subqueries into joins on SQLite (#38692)