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
Optimize Find/Single operations with includes #22579
In 5.0 RC1 a warning log message is thrown for .SingleAsync() queries:
warn: Microsoft.EntityFrameworkCore.Query[10102]
Query uses a row limiting operation (Skip/Take) without OrderBy, which may lead to unpredictable results.
I guess this has something to do with the fact that the actual SQL generated includes an automatic TOP(2) clause to determine if there are more than 1 item matching the query.
In 3.1 there were no warnings logged. I prefer no warnings logged.
Reacted by Zero3, D. St-Jacques, Mehdi Saeedifar, ghosttie, Jorge Morales, Piotr Wosiek, Dan Conley, Patricia Cadenas, PauliusLe, Mahmoud Kandeel and 15 more
The warning is logged because doing Single on a query without using Where and OrderBy, which means that the query is going to get 1 record from server which can change in future iteration of query. This warning existed in 2.x release and was absent in 3.1. We have just reintroduced what was missing. To avoid warning, add ~Where/~OrderBy to the query before calling Single.
Error on my part that it should not logged with Where. It does get logged in the absence of OrderBy when translating Skip or Take. SingleAsync does not use Skip/Take pipeline directly, can you share full query?
Additional queries for Include has to do Take(1) to only get correlated data for first result they don't have OrderBy so result could be non-deterministic. For Single we may be able to identify that the predicate is on PK so there would be only one match, it is hard to generalize it for any Where/Take combination. Queries can have multiple rows with same PK when doing joins and other complex scenarios. This was probably happening in 2.1 also.
Still trying to wrap my head around this. From a user perspective, something feels wrong since I expect to get the single user with all included navigation properties without warnings or having to add OrderBy.
I also don't really understand why the Include have to do Take(1) ? I thought it would just match by the predicate given, joining any related items.
But technical reasons aside, what is the recommended way to retrieve a single tracked item from the database by primary key having additional navigation properties eagerly loaded?
I also don't really understand why the Include have to do Take(1) ? I thought it would just match by the predicate given, joining any related items.
It does predicate match but it needs to figure out which items from parent it needs to match in the predicate joining to Children. If only 1 (or limited number of parents) are selected then we have to restrict the set of parents to same amount in additional queries so that predicate only matches related data for that limited set rather than generating join for everything.
You can suppress the warning or add OrderBy before First.
We will discuss in the meeting "recommended way".
Reacted by Joakim Riedel, D. St-Jacques and Jorge Morales
Thanks. I'll try to suppress it for now, don't want any possible overhead of OrderBy since this is on a critical application path. How do I suppress a single warning? I do want warnings for other Single-query abuses, just not this particular one.
Actually there will be OrderBy always because of Include so that we get records in a deterministic order to create proper buckets. It can be optimized for Single case but currently that is not happening.
Did a bit of debugging this morning and it adds an extra ORDER BY in the inner clause. But the sql query planner didn't seem to add much overhead from this, perhaps it's smart enough to optimize out the ORDER BY since the WHERE clause is on primary key?
SELECT [t].[Id], [t].[...]
FROM (
SELECT TOP(2) [a].[Id], [a].[...], [c].[Id] AS [Id0], [c].[...]
FROM [AspNetUsers] AS [a]
INNER JOIN [Currencies] AS [c] ON [a].[CurrencyId] = [c].[Id]
WHERE [a].[Id] = @__currentUserId_0
ORDER BY [a].[Id]
) AS [t]
ORDER BY [t].[Id], [t].[Id0]
query.SingleAsync(u => u.Id == currentUserId)
(warning)
SELECT [t].[Id], [t].[...]
FROM (
SELECT TOP(2) [a].[Id], [a].[...], [c].[Id] AS [Id0], [c].[...]
FROM [AspNetUsers] AS [a]
INNER JOIN [Currencies] AS [c] ON [a].[CurrencyId] = [c].[Id]
WHERE [a].[Id] = @__currentUserId_0
) AS [t]
ORDER BY [t].[Id], [t].[Id0]
I'll keep the OrderBy for now until your meeting to see if there's a better way. At least I do not get any runtime warnings now anymore.
changed the title [-]5.0 RC1 .SingleAsync() throws warning about row limiting operation[/-][+]Optimize Find/Single operations with includes[/+]on Sep 18, 2020
For your info - I also did some basic StopWatch-benchmarking of the SingleAsync query with Include compared to FindAsync followed by LoadAsync, but the SingleAsync still performed slightly better (~38 ms compared to ~43 ms) even with the ORDER BY.
An optimized FindAsync with Include would be handy for querying out single items and related data, looking forward to that!
I just want to add that aside from having a warning where it's preferable to see none, this also makes it harder/near impossible to track down actual cases where non-deterministic results are possible because of, e.g., .FirstAsync() when the logs are full of the false positives.
Reacted by Daniel Betts, jmalatia, Piotr Wosiek, Dan Conley, Shrey Kumar, Rumen Kasabov, Mike Tinnes, Sascha Sternheim, Jan Trejbal, Yannik Lauenstein and 16 more
@ajcvickers friendly bump - would this be considered a candidate to move from backlog anytime soon? as you see in previous comment from 2021, there's at least 22 other souls looking for this.
@joakimriedel This issue is currently at position 131 in the list of most voted issues. Not sure why people are doing 👍 on the comment without voting for the issue, but even if it had 23 votes, it would still only be at around position 62.
Activity
smitpatel commentedon Sep 17, 2020
The warning is logged because doing
Singleon a query without usingandWhereOrderBy, which means that the query is going to get 1 record from server which can change in future iteration of query. This warning existed in 2.x release and was absent in 3.1. We have just reintroduced what was missing. To avoid warning, add ~Where/~OrderBy to the query before calling Single.joakimriedel commentedon Sep 17, 2020
This particular query just picks the current user by primary key, hence no Where/OrderBy.
.SingleAsync(u => u.Id == currentUserId)Meaning I want the single user by Id, but I want exceptions thrown if:
or
I don't see why I should add Where/OrderBy in this case?
smitpatel commentedon Sep 17, 2020
Error on my part that it should not logged with
Where. It does get logged in the absence ofOrderBywhen translatingSkiporTake.SingleAsyncdoes not useSkip/Takepipeline directly, can you share full query?joakimriedel commentedon Sep 17, 2020
Sure
smitpatel commentedon Sep 17, 2020
Additional queries for
Includehas to doTake(1)to only get correlated data for first result they don't have OrderBy so result could be non-deterministic. For Single we may be able to identify that the predicate is on PK so there would be only one match, it is hard to generalize it for any Where/Take combination. Queries can have multiple rows with same PK when doing joins and other complex scenarios. This was probably happening in 2.1 also.joakimriedel commentedon Sep 17, 2020
Still trying to wrap my head around this. From a user perspective, something feels wrong since I expect to get the single user with all included navigation properties without warnings or having to add
OrderBy.I also don't really understand why the
Includehave to doTake(1)? I thought it would just match by the predicate given, joining any related items.But technical reasons aside, what is the recommended way to retrieve a single tracked item from the database by primary key having additional navigation properties eagerly loaded?
smitpatel commentedon Sep 17, 2020
It does predicate match but it needs to figure out which items from parent it needs to match in the predicate joining to Children. If only 1 (or limited number of parents) are selected then we have to restrict the set of parents to same amount in additional queries so that predicate only matches related data for that limited set rather than generating join for everything.
You can suppress the warning or add OrderBy before First.
We will discuss in the meeting "recommended way".
joakimriedel commentedon Sep 17, 2020
Thanks. I'll try to suppress it for now, don't want any possible overhead of
OrderBysince this is on a critical application path. How do I suppress a single warning? I do want warnings for otherSingle-query abuses, just not this particular one.smitpatel commentedon Sep 17, 2020
Actually there will be OrderBy always because of Include so that we get records in a deterministic order to create proper buckets. It can be optimized for Single case but currently that is not happening.
joakimriedel commentedon Sep 18, 2020
Did a bit of debugging this morning and it adds an extra
ORDER BYin the inner clause. But the sql query planner didn't seem to add much overhead from this, perhaps it's smart enough to optimize out theORDER BYsince theWHEREclause is on primary key?query.OrderBy(u => u.Id).SingleAsync(u => u.Id == currentUserId)
(no warning)
query.SingleAsync(u => u.Id == currentUserId)
(warning)
I'll keep the OrderBy for now until your meeting to see if there's a better way. At least I do not get any runtime warnings now anymore.
ajcvickers commentedon Sep 18, 2020
Moving to the backlog to attempt to not generate this warning when it is not needed.
[-]5.0 RC1 .SingleAsync() throws warning about row limiting operation[/-][+]Optimize Find/Single operations with includes[/+]joakimriedel commentedon Sep 19, 2020
For your info - I also did some basic StopWatch-benchmarking of the
SingleAsyncquery withIncludecompared toFindAsyncfollowed byLoadAsync, but theSingleAsyncstill performed slightly better (~38 ms compared to ~43 ms) even with theORDER BY.An optimized
FindAsyncwithIncludewould be handy for querying out single items and related data, looking forward to that!mqudsi commentedon Feb 24, 2021
I just want to add that aside from having a warning where it's preferable to see none, this also makes it harder/near impossible to track down actual cases where non-deterministic results are possible because of, e.g.,
.FirstAsync()when the logs are full of the false positives.joakimriedel commentedon Aug 30, 2022
@ajcvickers friendly bump - would this be considered a candidate to move from backlog anytime soon? as you see in previous comment from 2021, there's at least 22 other souls looking for this.
ajcvickers commentedon Aug 30, 2022
@joakimriedel This issue is currently at position 131 in the list of most voted issues. Not sure why people are doing 👍 on the comment without voting for the issue, but even if it had 23 votes, it would still only be at around position 62.
joakimriedel commentedon Aug 30, 2022
Ok thanks, did not know it was such a strict popularity vote to get out of backlog. I'll see what I can do to promote it 😊
ajcvickers commentedon Aug 30, 2022
@joakimriedel It's not a strict popularity vote at all--see the planning process. But cases made on popularity need to be pretty high up the list.