How to design a rating system which permits one vote per user considering the performance?
I am using Ruby on Rails\MySQL and I would like to implement a rating system with the following condition开发者_StackOverflows\features considering a large amount of users and articles:
- Each article have 3 rating criterias
- Each criteria is a binary function (examples: good\bad, +1 \ -1, ...)
- Each user can vote one only time per criteria and per article
I would like to know in those conditions what are the best approaches\techniques to design\think of the database (for exmple in UML) in order to ensure a optimal performance (response time of a database query, CPU overloading, ...).
P.S.: I think a rating system as that working for the Stackoverflow website.
Create a table for the ratings:
CREATE TABLE vote
(
userId INT NOT NULL,
articleId INT NOT NULL,
criterion ENUM('language', 'usefulness', 'depth') NOT NULL, -- or whatever
value BOOLEAN NOT NULL,
PRIMARY KEY (userId, articleId, criterion)
)
This will allow each user to cast at most one vote per article per criterion.
criterion
has type enum
which allows only three different criteria. This constraint is on metadata level: this means that if you want to add the criteria, you will have to change the table's definition rather than data.
精彩评论