I’m using MySQL to add the ability for my users to ‘like’ content around my site (WordPress). I’ve hacked a plugin that does this by creating its own table and then adding an record for each like with the content ID and user ID.
Would it be more efficient to instead add a column to the user table for all liked content ID’s associated with each users, and to add a column to the content table for each user ID that has liked each piece of content? I would make different queries depending on whether I’m displaying a list of a user’s favorites or the # of likes for a piece of content.
I’m new to MySQL so any advice would be much appreciated 🙂
Probably no,
you can have a post have lots of users like,
or an user has liked many posts,
you would probably exceed the column length very soon
The common practice is using a separate table to store the relation :-
Build two indexes (user_id, add_date), and (post_id, add_date).
With the simple index,
you can get list of posts liked an user order by add_date desc easily,
or vice versus, to get list of users liked a post order by add_date desc
If you wants to listed most liked posts, then an index on (post_id, user_id) can be considered