devconnector_2.0 icon indicating copy to clipboard operation
devconnector_2.0 copied to clipboard

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

Open john012343210 opened this issue 3 years ago • 5 comments

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?

john012343210 avatar Sep 27 '20 10:09 john012343210

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

MananDesai54 avatar Oct 29 '20 02:10 MananDesai54

@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).

john012343210 avatar Oct 29 '20 02:10 john012343210

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

MananDesai54 avatar Oct 29 '20 02:10 MananDesai54

@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

manas2297 avatar Dec 27 '20 21:12 manas2297

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

MananDesai54 avatar Dec 28 '20 04:12 MananDesai54