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

database read performance while getting all the post that a user has comment to #168

Open
john012343210 opened this issue Sep 27, 2020 · 5 comments
Labels
question Further information is requested stale

Comments

@john012343210
Copy link

What if I want to extend the project a little bit,
that is a page showing all post that the logged in user has comment to,
is it not "that efficient" if we have to scan the whole mongo database and see whether the comments property contains the comment object belonged the logged in user ?
Since if the user only comments on one post and there are 100000posts in the database, this sounds not very efficient?

@MananDesai54
Copy link

yes this sounds good. we can do one thing, first we don't fetch comments. whenever user press on comments toggle button we can fetch comments for that post only @AronWater @bradtraversy

@john012343210
Copy link
Author

@MananDesai54 I do think we are thinking about a different matter, sometimes user( let say someone named John) might want to know what post he has "commented on". But let say there are 1000000 posts in the mongo database, the user has only commented to one post only, then we have to do a full database scan to see which of the posts object have the comment field containing the user document referring to John. This sound not very efficient. But if we store this information in a separate user collection, there will be duplication of data, which leads to the problem of atomicity (which is a concept in the database). When some user comments on one post, the database has to both update that posts document( to insert a comment document) and also the user document referring to that user( insert that posts that the user has just now commented to).

@MananDesai54
Copy link

Oh now I get you. So for that we can add one field of user reaction in user profile. So we can find posts user liked and posts user commented @AronWater

@manas2297
Copy link

@MananDesai54 agree with the solution of adding another field in the user profile, but is i wonder if there is any other way or this is the best possible solution? @bradtraversy @AronWater

@MananDesai54
Copy link

we have user table and post table, we can create one mid table for connecting user with posts with user. We can have table with userID, his postsId array and reacted PostArray. reacted postArray is array of object { postId: string, typeOfReaction: 'comment' | 'like' } then we can know it easily right. How you think of it ? @manas2297 @AronWater @bradtraversy

@bushblade bushblade added the question Further information is requested label Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

4 participants