Skip to content
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

Consider a related entity loading strategy that aggregates dependent collections to a JSON array #30958

Open
roji opened this issue May 23, 2023 · 1 comment

Comments

@roji
Copy link
Member

roji commented May 23, 2023

In EF Core 7.0, we're adding type mapping APIs to precisely control how values are serialized/deserialized from JSON; this should allow us to represent any (primitive) value in JSON and reliably serialize/deserialize it to stored documents in the database.

Databases also allow aggregating multiple rows into a single JSON document. On most databases, this is done using an aggregate method (jsonb_agg on PostgreSQL, json_group_array and json_object on SQLite, JSON_ARRAYAGG on MySQL); we added support for custom provider aggregate methods in EF 7.0 as well (#22957). On SQL Server this is done via the special FOR JSON construct.

Combining these features, we could load a related collection navigation by aggregating it to a single JSON text, and deserialize it back on the client. This works around the cartesian explosion and data duplication problems inherent in single queries with JOINs, but also doesn't suffer from the problems of split queries (increased memory needs due to buffering of the principal results, loss of transactional consistency).

On PostgreSQL we'd aggregate to an array instead (no JSON needed; tracked by npgsql/efcore.pg#1691).

@roji roji changed the title Consider a related entity loading strategy that uses aggregates dependent collections to a JSON array Consider a related entity loading strategy that aggregates dependent collections to a JSON array May 24, 2023
@ajcvickers ajcvickers added this to the Backlog milestone May 24, 2023
@roji roji added the area-json label Jul 9, 2023
@roji
Copy link
Member Author

roji commented Apr 27, 2024

Note: the same concept can be used for translating final GroupBy without Select: the groups can be aggregated to JSON arrays, transferred to the client and the JSON parsed by EF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants