Forum Moderators: open

Message Too Old, No Replies

Creating a ranking system

I need an efficient algorythm

         

Moosetick

8:52 pm on May 24, 2006 (gmt 0)

10+ Year Member



I'm making a site where users will rate pictures. I want to randomly show a group of pictures to a user and they pick the one they like the most. I don't want anyone to be able to choose the same picture more than once. I'm not sure what the best way to store all this info in a database would be that could scale to 1000+ users and 1000+ pictures. Here are my thoughts. Obviously I want this to be efficient and not grind the server to a halt once the volume increases.

1. I could have a table with 2 fields. One has the picture ID. The other has a list of everyone who has rated the picture seperated by commas. The server would parse that list and dump all the names into an array that can be checked.

2. Have a table with a field for the user ID. Also have a field listing all pictures reviewed seperated by commas. Again, parse and check that list.

I suspect there is a better way to do this but I can't think of one at the moment. Any suggestions?

mattglet

9:22 pm on May 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pictures Table
--------
ID

Users Table
--------
ID

UsersToPictures Table
---------
UserID
PictureID

When you want to check if the user has already voted, run this sort of query:

SELECT TOP 1 ID FROM Pictures WHERE ID NOT IN (SELECT PictureID FROM UsersToPictures WHERE UserID = TheUserIDHere AND PictureID = ThePictureIDHere)

The record that is returned will be the ID of a picture that is eligible to be voted on by the User (they have not already voted on it). If there is no record returned, they have voted on all the pictures. Just make sure you have proper indexes on your columns.

Moosetick

10:27 pm on May 24, 2006 (gmt 0)

10+ Year Member



SELECT TOP 1 ID FROM Pictures WHERE ID NOT IN (SELECT PictureID FROM UsersToPictures WHERE UserID = TheUserIDHere AND PictureID = ThePictureIDHere)

OK, I get the first select statement gets the first item that does not show listed in the UsersToPictures table for a particular user. The 2nd part looks like it produces a list of all the pictures that the user has viewed so far. The part I'm not sure about is the end of the 2nd statement. Why do you need "AND PictureID = ThePictureIDHere"? Also, what exactly is ThePictureIDHere?

Thanks so far for the help!

mattglet

2:49 pm on May 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, you're right about that... no need for the 2nd WHERE condition.

TheUserIDHere and ThePictureIDHere are just place holders for you to plug in the relevant IDs you need to query upon.