I have object which store in database, it's a some text with properties. That text has rating. I need to store this rating, and prevent to one user raise this raiting more than one time. If I store "text id" and "user id" in other table and count all records which have needing "text id" i have too much records in table.
-
1Too much records? How many do you have and how many do you feel is too much? – hd1 Jan 08 '13 at 02:00
-
If I have 1,000 users and 1,000 posts, and each post have 1,000 rating I have 1,000,000 rows in database. It doesn't so much? I want to store fewer records. – Deadbyelpy Jan 08 '13 at 02:15
-
Not at all... This is precisely the use case for an RDBMS. When you get to the hundreds of millions of rows, then a case can be made that it's `too much`, and then you graduate to things like [Hive](http://hive.apache.org) or [Cassandra](http://cassandra.apache.org). – hd1 Jan 08 '13 at 02:18
-
Thanks, I'll think about it. – Deadbyelpy Jan 08 '13 at 02:23
3 Answers
There are two ways:
You can use many-to-many relationship ie use separate table with name like 'user_likes', it will have
user_id
andlike_id
columns, both of them are primary key (it makes possible user to like the like_object only once)Another way - which hightraffic websites use: every user record in user table has columns: likes which is just serialized array or json, whatever. Before update this columns your application retrieve this data and look for particular
like_object_id
if it doesn't exist - you update your database. Please note that in this case all care about data consistency in your application (for instance like_object_id exists in some user record, but doesn't exist in like_object table) should be implemented in your application code, not database.
P.S. Sorry for my english, but I tried to explain as best as I could.

- 2,514
- 2
- 22
- 41
If I store "text id" and "user id" in other table and count all records which have needing "text id" i have too much records in table.
How do you know what is too many records?
Some of the MySQL tables I support have billions of rows. If they need more than that, they split the data to multiple MySQL servers. 1 million rows is not a problem for a MySQL database.
If you want to limit the data so each user can "like" a given text only once, you must store the data separately for each user. This is also true if a user can "unlike" a text they had previously liked.
CREATE TABLE likes (
user_id BIGINT UNSIGNED NOT NULL,
post_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (user_id, post_id),
KEY (post_id, user_id)
);
This example table uses its primary key constraint to ensure each user can like a given post only once. By adding a second index, this helps to optimize queries for likes on a specific post.
This is only 16 bytes per row, plus the size of the index. I filled an InnoDB table with over 1 million rows, and it uses about 60MB.
mysql> show table status\G
Name: likes
Engine: InnoDB
Rows: 1046760
Data_length: 39419904
Index_length: 23658496
It's common to store databases on terabyte-sized storage these days, so a 60MB table doesn't seem too large.

- 538,548
- 86
- 673
- 828
-
isn't this requires an extra request to the server, that we can avoid if we just add the ids as comma separated string? – Ahmad Ali Aug 20 '21 at 20:21
-
@AhmadAli, Yes, denormalization can improve a query if you want to fetch _all_ the ids. But what if you want to do some different type of query? I recommend you read my answer to [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/a/3653574/20860). Denormalization improves one type of query, as it makes other types of queries worse. – Bill Karwin Aug 20 '21 at 22:16
I store the likes with the post itself, but not sure with its performance since non of my websites reached a very heavy load.
but I do the following :
Post {
id int;
likes_count int; // likes count to quickly retrive it
likes string; // id of the users liked this post, comma separated
}
when a user likes a post, (using ajax):
the UI will update directly and show that the user liked the post
ajax will send request to the server with the post id and the user id, then post data will be updated as follow:
post.likes_count += 1;
post.likes += userId + ',' ;
when the user reload the page, it will check if his id is in likes, then it the post will appear as liked
.

- 704
- 1
- 10
- 28