Skip to content

One to many relation loading optimization #25

@farid-fereidooni

Description

@farid-fereidooni

For updating a post comment following code is written:

var post = await _ctx.Posts
.Include(p => p.Comments)
.FirstOrDefaultAsync(p => p.PostId == request.PostId, cancellationToken);

For example, if the post has more than 2000 comments, This code will load all of them into memory.
While we need to update only one comment.

Wouldn't it be better to do it as this:

var post = await _ctx.Posts
    .Include(p => p.Comments.Where(_ => _.CommentId == request.CommentId))
    .FirstOrDefaultAsync(p => p.PostId == request.PostId, cancellationToken);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions