-
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: correlated collection optimization is broken for queries containing top level First/FirstOrDefault/Single/SingleOrDefault #10813
Comments
…or queries containing top level First/FirstOrDefault/Single/SingleOrDefault Problem was that correlated collection optimization rewrite wasn't properly handling result operators that could be part of the parent query model. Parent QM is cloned, rewritten and then used as part of the inner collection query. Fix is to properly compensate for result operators that change the cardinality of the result (specifically First/Single/Last), similarly to what we do in the include pipeline.
In the presence of Single value from sequence result operator, we should not be doing any optimization. Rather than we should just use N+1 mode of evaluation since it would run 2 queries anyway. It gives us benefit that 2nd query is filtered based on a parameter rather than a join (also no order by is needed in 2nd query) which would be efficient than running a normal collection query. |
@smitpatel correlated collections optimization doesn't work if the outer is using outer parameter, so in case of multi-level correlation we would be producing inefficient queries. We could make the optimization you suggest once/if #10877 is addressed |
…or queries containing top level First/FirstOrDefault/Single/SingleOrDefault Problem was that correlated collection optimization rewrite wasn't properly handling result operators that could be part of the parent query model. Parent QM is cloned, rewritten and then used as part of the inner collection query. Fix is to properly compensate for result operators that change the cardinality of the result (specifically First/Single/Last), similarly to what we do in the include pipeline.
fixed in d787c13 |
Re-opening this. This is partially fixed. We need tracking issue to do outer parameter injection for single record collection query. Either we use this or file a new one. For include pipeline (perhaps older one) #2182 |
@smitpatel #10877 is already tracking this (see one of the comments) |
Workaround: First/FirstOrDefault: var result = ctx.Parents.Select(p =>
new
{
Children = p.Children
}).Take(1).ToList().FirstOrDefault(); Single/SingleOrDefault: var result = ctx.Parents.Select(p =>
new
{
Children = p.Children
}).Take(2).ToList().SingleOrDefault(); |
Repro:
Problem here is that when we rewrite QM to create query that fetches inner elements, we produce join between inner elements and cloned parent query model. However, if parent model contains result operators that change cardinality, we would clone it also and hence produce invalid QM.
The text was updated successfully, but these errors were encountered: