简体   繁体   中英

Best way mysql storing votes with php voting sytem

hi there i have a php voting script with ratings table which is used for multiple categories article, images and comments

logged in user only can rate as liked it or disliked_it

is the best way to store users who rated should be by storing them in long text like this

54654,5145463,19993,3215,69445 

or making a new table that link rate_id with user_id

and what is the effect on script and mysql performance

rate_id     int(11) unsigned    NO  PRI     NULL    auto_increment
rate_for_id     int(11) unsigned    NO NULL      
rate_for    enum('article','image','comment')   NO NULL      
liked_it    int(5) unsigned     NO NULL      
disliked_it     int(5) unsigned     NO NULL      
users_rated     longtext    NO      NULL    

Definitely make it a seperate table. Storing multiple values in a single field is almost always bad design, and makes it hard to process later. eg To check if someone's voted already, you have to do substring searching into your "all-in-one" field. Whereas if you use a separate user_votes table, you just check if a record exists.

Performance hit is hard to quantity. It's very situational and hardware/software dependent. Try both methods and see which is better in your case, but I'll guess that it'll be the separate table option that wins.

The problem with storing multiple values in the same field is that it unnormalizes your database, thus makes it harder for you to extract the data. For example, when you need to select a record with a certain like_it's ID (say, getting all the topics that this ID liked), you can't really get it since it is mixed with a lot of other values.

So, in order to make your database normalized, I'd strongly recommend you to separate it into another table, so that you can extract the data much more easily for any later needs. That is the point of relational database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM