2

I've run into bit of an issue on implementing a count system in my project.

I have a system where users can like a post. Should I keep a count of it in the same table like

enter image description here

or should I remove the like_count column from the post table.

My concern if I have like_count column in the post table is -- frequent update of the value.

If I choose not to add a like_count column in the post table, I'd have to use count() frequently to display the details. I've to design a data model for thousands of users. So which model is the right way to implement?

P.S: Please ignore the data types of the columns. I just did a quick version of it to post it here.

1
  • You are doing caching, and, denormalizing you data model.. Perfectly valid but only when warrented, should not be your first choice.
    – Erik Eidt
    Commented Dec 15, 2016 at 15:49

2 Answers 2

4

First:

  • In the diagram post_id is the PK of PostLikes forcing any post to have only one like. PK should be (post_id,user_id)
  • Table names should be singular, so PostLikes should be named POST_LIKE.

That said, my recommendation is:

A like counting feature is not a critical one (like, say the balance of a bank account) -- meaning, it is not critical to have the very exact like count of a post at any given second -- so in that case I suggest you to keep the LIKE table as well as the like_count column in POST and update it every N minutes. You don't even have to update every post's like count every N minutes. Every run of the batch process can update the count of a group of posts or the most recently liked ones.

Please note that with such a column you are knowingly de-normalizing for performance's sake, which is OK if well sustained.

6
  • Thank your for answer. As I had mentioned, I just made up a quick diagram for illustration purposes. In the actual table, the PK is as you had stated and the table names too. Would your recommendation be any different if I were to make all my reads(select statements) from a SLAVE and only DDL in Master Commented Dec 15, 2016 at 12:08
  • Thank you for the edit. May I ask what would be the normalised way of doing it? Commented Dec 15, 2016 at 12:13
  • I guess you mean DML. In that case my recommendation would be the same. Commented Dec 15, 2016 at 12:13
  • @MageshKumaar The normalized version is not having a calculated column in any table and doing the count() every time you need it. That could be a performance issue if the table has no proper indexes or the table has billions of rows, since RDBMS don't scale well, performance-wise, into the billions of rows. Commented Dec 15, 2016 at 12:15
  • I've decided to go with the normalized version of this since table growth is not expected to be as large as that. Thank you for your help. Just one more thing, for a NON-RDBMS solution, which would you recommend ? Commented Dec 15, 2016 at 12:24
1

Don't optimize ahead of time.

Essentially you are trading a "Count" for more frequents writes to your "Post" table and having to keep things in sync. I would remove the "like_count" column for now and write System tests to profile your applications performance.

If it becomes a performance problem you can optimize at that point.

EDIT: Didn't realize it was already a performance problem.

From a performance point of view there is just not enough information here to say which would perform better overall.

EDIT2: changed my mind

From a design point of view it makes more sense keeping it on the "posts" table if you are unable to optimize it away.

5
  • Thanks for your answer. I got into this dilemma because of the scalability issue. I'd remove like_count column too from the posts table. Commented Dec 15, 2016 at 10:45
  • @MageshKumaar Hmm would be curious what the query looks like, how many rows in each table there are, and what the runtime of the query is. Also are you using any locks that would get in the way of database read operations?
    – Adrian
    Commented Dec 15, 2016 at 10:58
  • This is still in the design phase so actual queries for reading the data does not exist (yet). I'd like to fix the DB design for the most part of it before proceeding with the actual code. Commented Dec 15, 2016 at 12:10
  • Didn't notice your edit before. I agree that there's not enough information since it's just a design problem. Would you still go with the removal of the like_count column from the posts table? Commented Dec 15, 2016 at 12:11
  • @MageshKumaar Since you are de-normalizing things it probably makes more sense to keep it on the "posts" table. Your "READ" operations will get the bulk of the benefit which is what is driving this anyway.
    – Adrian
    Commented Dec 15, 2016 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.