Keep a table of votes . For instance. votes (comment_id, user_id, value, stamp) - the value is -1 or +1.
This allows for accountability, and you can do a UNIQUE index on (comment_id, user_id) to prevent re-voting. You can also easily delete the user and all your votes if he / she sends spam.
To sort comments by account, you can make a JOIN between comments and voting tables and use SUM / GROUP BY to get a total score. However, this can be slow. For speed, you can also save the rating field in the comment table. Each time a new row is added to the vote table, you add / subtract 1 from the comment rating.
Since you keep every vote in the table, itβs easy to recount the bill on demand. Overflowing does something similar with reputation - the overall reputation rating for the user is cached and recounted so often.
source share