Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:32:17+00:00 2026-06-17T10:32:17+00:00

Lets say I have three normalized tables, one for threads, one for comments, and

  • 0

Lets say I have three normalized tables, one for threads, one for comments, and one that connects the two.

I want to display the number of comments in a thread, and that involves finding every comment belonging to a certain thread.

Obviously, I don’t want to do this query every time i display a page, so I need to cache the number of comments to a thread. My two options (as I see it) are:

  1. Add a number_comments row to the threads table, and update it whenever adding / removing a comment.

  2. Cache the value in memory, either by telling mysql to cache it or using something like APC / memchached

What are the pro’s / con’s of each?

I’m thinking the first one is simple, a bit less performance but you have redundancy and storage is a lot cheaper than memory, though it does muck up the database with a “dynamic”, ever changing value (also note that I’ll need to save “upvotes” for comments, so this question applys to more than one “dynamic” value).

The second one is better performance, but it introduces a new technology that you have to tie in just to cache the number of things.

This project would have a relatively low amount of users, but I want to know which is preferable to a highly visited site as well (e.g. how does facebook store number of comments [I’m guessing both database and in memory]).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:32:19+00:00Added an answer on June 17, 2026 at 10:32 am

    Keep in mind this quote from Donald Knuth:

    Premature optimization is the root of all evil.

    I think caching, or “database denormalization” in this case, is a perfectly valid option, but it’s an option that is best considered when the “normal” approaches are no longer adequate.

    You say that you “obviously” don’t want to run an extra query to grab the comment count on every page view, but it’s actually not that obvious. If your database is properly configured, you should already have an index on the thread_id field in your comments table (or whatever you happen to call the field). Running a query based on an indexed field, particularly when the query only returns the a generated COUNT() field rather than a huge list of threads, doesn’t actually have a lot of overhead. I think it’s easier to just run that query and be done with it.

    That said, there is value in denormalizing a database when performance reasons demand it. In that case, I would add a comments_count field to the thread table which is incremented whenever a new record is added or deleted from the table. You need to remember to add extra code surrounding your INSERT and DELETE queries, and possibly to UPDATE queries as well, ŀŀdepending on if your comments table keeps track of active/deleted states.

    Again, this is a premature optimization in most cases. The question you need to ask yourself is, “is this site so busy/under such heavy load that the added complexity of managing a computed field is less costly than just running a quick COUNT() query?” If it is, then by all means go for the denormalization route, but it probably shouldn’t be the first option you go for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
As shown in title, lets say that I have two projects in one solution.
Lets say I have random $variable and three arrays: $array1 = (one, two, three);
Lets say I have an array of bad words: $badwords = array(one, two, three);
Lets just say that I have three textboxes: TextBox1, TextBox2, TextBox3. Normally if I
Lets say I have one row with three columns - some buttons on left
Let's say I have the following xml: <one> <two> <three> <three> foo </three> <four
Lets say I have three models/tables: operating_systems , words , and programming_languages : #
I have three tables, we'll call them table1, table2, and table3. Lets say each
Lets say I have three webpages - one for big cars, one for middle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.