118 reputation
4
bio website
location United Kingdom
age 37
visits member for 4 years
seen Nov 5 at 11:12

Jan
17
awarded  Supporter
Jan
12
awarded  Autobiographer
Jan
11
awarded  Scholar
Jan
11
accepted What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
@ZaphodBeeblebrox great. Thanks for your time!
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
@ZaphodBeeblebrox Let's say you want to show a blog post. Each blog post will have an author, will have multiple tags, will have multiple comments etc. So if you only want to display blog title and the total number of comments, there is no need to load all comments from db. Hope this is clearer.
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
@ZaphodBeeblebrox, so number of votes are relevant to question and it's displayed whenever I display the question, so it should be in the questions table, right? If I want to display the actual votes, then I can query the votes table and get the votes.
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
what I mean, when you retrieve a model from db, do you retrieve whole related models, although this is not our discussion here ;)
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
@ZaphodBeeblebrox Well there is nothing specific I am building. I am asking this question as I always come across this and always wondered how others do it. I am reading about Domain Driven Design and when I see term object graph this came to my mind :)
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
@DanMcGrath, yes, all votes will be saved in a separate table for obvious reasons. Q table will only record the number of votes for the questions.
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
Sorry, I didn't see the second part of your answer :) instead of cron job, would it be better to increase the VoteCounter field in the questions table whenever user up votes it? If cron job is used, again it needs to scan a huge table to calculate the counters
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
I think keeping the vote count/comment count (or any similar integer counters) in the same table as the post sounds better solution. But what if I need to display the user information for each post (i.e. authorname)? of course authorid will be in the posts table but to get the author name, I need to go back to db for each post (again, assume no caching)
Jan
11
comment What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?
Thanks for the reply Zaphod, but I really think this is a very important decision to make during the initial development. As you know db queries are the most common bottleneck of all. Let's say if I decide to calculate the votes on the fly, it means if I display 10 posts per page it means 10 extra db query (assuming there is no caching at all). But if I keep the counters in the same table as the post, there won't be any extra query needed.
Jan
11
awarded  Student
Jan
11
asked What are the advantages and disadvantages of storing the number of comments in an integer column vs. calculating it on the fly?