Take the 2-minute tour ×

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.

share|improve this question
add comment

closed as not constructive by bkaid, Will Nov 4 '11 at 15:42


1 Answer

up vote 1 down vote accepted

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:

    Post_ID int FOREIGN KEY REFERENCES Posts(Post_ID),
    User_ID int FOREIGN KEY REFERENCES Users(User_ID),
    created_date datetime,
    CONSTRAINT PKC__Post_Likes__Post_id__User_id PRIMARY KEY (Post_id, User_id)

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 Posts record so you don't have to JOIN to the child table and SUM all 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.

share|improve this answer
add comment
