Could you please describe the best way to create a Facebook “like” feature table where I can store the likes of users and how? (columsn, pks, etcs)
Also if it’s possible I’d like to know what’s the best and fastest method to retrieve the data from that table.
Thank you.
Because “likes” can be added/removed, they are related to a specific user, and a user can typically only “like” something once, it makes sense to create a table like this:
There are lots of considerations here. post_id and user_id together make a good natural key but will be prone to fragmentation a bit. Having a separate IDENTITY key would not add much as the important index on post_Id would still get fragmented. It may be worth having another index on User_id so the optimizer can quickly find a users’s like but it wolud be better to tune with some real data and query plans.
In addition to this table, I would cache the running total of likes in the
Postsrecord so you don’t have toJOINto the child table andSUMall the likes every time you display something, that can get costly quick. You can maintain that value with a trigger or put the code to maintain that value in your DAL.