When returning IQueryable to OData, all results are drained into memory before the first is returned to the client.
We are returning an IEnumerator (via the implementation of IQueryable), which does properly implement "yield return" and thus lazy enumeration to OData:
WebApi:
[EnableQuery( EnsureStableOrdering = false, AllowedQueryOptions = (AllowedQueryOptions)(AllowedQueryOptions.Supported - AllowedQueryOptions.Skip - AllowedQueryOptions.Count))] public async Task<IQueryable<ScenarioDto>> Get()
IQueryable implementation (wraps DocumentDB's IQueryable):
public override IEnumerator<TElement> ExecuteQuery<TElement>(Expression expression) { IQueryable<TElement> query = underlyingProvider.CreateQuery<TElement>(expression); var enumerable = DocumentCollectionClient<Document>.StreamQueryWithContinuationAndRetryStatic(query, maxTime, maxRetries); return enumerable.GetEnumerator(); // <-- this ****DOES stream via "yield return"**** }
However, experiments show that even as we stream data out of the IQueryable (via the IEnumerator) back into OData, nothing is returned to the client until the last record has been read out of the IEnumerator.
Our data set is > 2 GB in size so this poses problems for us as OData is caching this entire dataset in memory instead of streaming it out to the client as expected.
It's like in this scenario we need a writer.flush() after
entrySerializer.WriteObjectInline(entry, elementType, writer, writeContext);
in ODataFeedSerializer.
Thanks Fan, I will try to make another pull request soon.
@nickkin-msft can you use page size to get this work around?
Haven’t tried it. That might work for well-behaved callers, but it wouldn’t prevent anyone (malicious, or who simply don’t know about the bug) from being able to make harmful calls into our service.
@nickkin-msft pageSize is set by server side, maybe you can try it.
[EnableQuery(PageSize=10)]
It appears this is not a valid workaround for us as paging seems to try to apply operations to the IQueryable (in our case supplied by DocumentDB) which it does not support.
Any further word on this? I have an OData endpoint hanging because of about 20MB of data in the IQueryable I want to return from the endpoint.
I have a table with + 1Milion records. I will need to abort OData usage? (OMG)... Using Entity Framework 6
Only if you want to return 1+ million records in a single call? I haven't had time to commit a fix for this as a consumer myself yet, but it should be very simple and is open for contribution.
This issue could be related https://github.com/OData/WebApi/issues/898 ? I dont know how to do.
I am in a situation where OData has become the required protocol for data retrieval from web clients. I work with large amounts of data so paging of the data is performed in stored procedures. Due to performance and resource concerns, I refuse to over fetch data from the procedures to play nicely with the built in paging functionality in the MS OData library.
I have tried returning an instance of PageResultOfT with the "count" set manually but the runtime appears to be overriding the value and applying the $skip/$top paging logic to the already paged values. This results in every page beyond the first returing no values because the runtime skips them all.
I have found a way to completely control the paging after several maddening days of testing.
The root cause of my issue was the EnableQuery attribute. It appears that this attribute instructs the runtime to perform paging operations, including total count override, on the PageResultOfT instance returned from the API method. Removing the EnableQuery attribute from the controller resolved the issue.
Note: The "Global Query Setting" heading in section 13.1 of the OData documentation only applies to methods that use the EnableQuery attribute, including times when it is inherited from the controller.