Jump to content

Ratings and Averages


cmgmyr

Recommended Posts

I'm making a "Hot or Not" site for one of my clients and I was just thinking about the rating system. How would the best way to handle a LOT of ratings and averages?

 

Option 1:

List everything in one big table. (id, userid, rating) then when a query is made it has to scan through all of this and get the average.

 

Option 2:

Use same table as above but add a "master" ratings table. (userid, total_rating, total_count) So that when I only have 1 row to query instead of 10,000+...

 

The reason why I want to keep the one big table is to keep track of who voted and what they rated but I know if I just kept this one only eventually it will slow everything down. I'm kind of leaning toward option 2, but that means that I would have to do and insert/update twice.

 

So...do you forsee any problems with using option 2? Do you have any other suggestions?

 

Thanks,

-Chris

Link to comment
https://forums.phpfreaks.com/topic/45667-ratings-and-averages/
Share on other sites

  • 2 weeks later...

I have created a limit thing to hot or not for springbreakvillage.com.  The way i have it is there is one tabel the hold submissions(or whatever si being voted on).  I have another table that then hold all the rating for all submissions linked by the submission id.  so if i want to find out how many times a submission has been voted on and the average votes, i just do:

 

<?php
$query = "SELECT COUNT(id), ADD(rating) FROM ratings WHERE submission_id = '{$submission_id}' GROUP BY submission_id"
list($total_votes, $total_rating) = array_values($db->GetRow($query);
$average = ($total_votes/$total_rating);
?>

 

This seems to be the best way i could come up with.  You can also track the user_id inside the ratings table to make sure the person can onyl vote once on a submission and stuff like that.  Thsi also allows you to even break down and seem how many rating of 10, or 9, or 8, or 7, and so on a submission has received.

Link to comment
https://forums.phpfreaks.com/topic/45667-ratings-and-averages/#findComment-231281
Share on other sites

<?php
$query = "SELECT (COUNT(id) / ADD(rating)) as avg FROM ratings WHERE submission_id = '{$submission_id}' GROUP BY submission_id"
list($average) = array_values($db->GetRow($query);
?>

 

...will save you a clock cycle or three.

 

Best,

 

Patrick

 

true but from my clients needs, they wanted to shwo the total number of votes for a submission so i needed to so it this way.

Link to comment
https://forums.phpfreaks.com/topic/45667-ratings-and-averages/#findComment-231338
Share on other sites

true but from my clients needs, they wanted to shwo the total number of votes for a submission so i needed to so it this way.

 

I don't see how that justifies fetching 'raw' data from a database, instead of keeping generation of the needed data in the database... The rule of thumb is: if the database can do it, let it.

 

 

Link to comment
https://forums.phpfreaks.com/topic/45667-ratings-and-averages/#findComment-231354
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.