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

  • Home
  • SEARCH
  • 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 6660705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:11:18+00:00 2026-05-26T02:11:18+00:00

i have a question probably you will consider it stupid but anyway while making

  • 0

i have a question probably you will consider it stupid but anyway

while making in php comments , i thought that would be cool if people can rate a comment with +1/-1 so i did one Table in the DB for the comments (id, user_id, comment, time) and another one for the rating comments (id_rate, id_comment, id_user, time) i use the id_user so he can not rate two times the same comment

well the problem is the following: if a blog post has like 100 comments and each has like 200 votes (+1 or -1) wouldn’t be the page too slow to load, lots and lots of queries just to load a few comments?

is another way around to do this following the normal forms of DBs?

Thanks for your time, i really appreciate it.

  • 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-05-26T02:11:19+00:00Added an answer on May 26, 2026 at 2:11 am

    To get the comments and their ratings can be done in a single SQL-query. It will be slow when you have A LOT of comments and ratings, but not for the numbers you mention.

    If you want to make it faster, you can create an attribute on the comments named “rating” or something like that, and update it every hour (or how often you think will suffice) or so. It will not be exact, but it will approximate the commentrating. But we are talking millions of comments and ratings here before it is nescesarry.

    This would normally be done with a materialized view, but that is unfortunately not implemented in MySQL… You can use the method described above though by writing a php-script and running it through a cronjob.

    Use a query like this (untested):

    SELECT * 
    FROM comments, ratings 
    WHERE comments.id = ratings.id_comment;
    

    assuming you add a value to your votes (+1 or -1) you can group them and calculate the total commentrating in one query like this (untested):

    SELECT comments.id, comments.user_id, comments.comment, 
           comments_time, SUM(ratings.value) AS rating
    FROM comments, ratings
    WHERE comments.id = ratings.id_comment
    GROUP BY comments.id;
    

    You will now have all the commentfields and the calculated commentrating as the field rating. If you want to speedtest it you can try to insert a million dummy comments and a million ratings and see how long the query takes.

    To speed it up even more you can add an index on comments.id_comment like this:

    CREATE INDEX speedthisthingup ON comments(id_comment);
    

    That should help the execution time a great deal 🙂

    Good luck!

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

Sidebar

Related Questions

This is a quick (and probably stupid) question but if I have two consts
This is probably a really stupid MSBuild question but if I have <ItemGroup> <Dll
I realize that this is a question that will probably not have a single
I have a general question that was probably answered somewhere here, but using search
So, here's a question that will probably have the SQL experts jumping all over
This will probably seem like a noob question but I have managed to intall
This is confusing me, so this question will probably be confusing. I have a
This is probably a simple question, but i have been unable to find a
This is probably a silly question, but here it goes.Imagine you have the following
I have what is probably a really simple, studid question but I can't find

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.