Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing likes,comment,views counter for a product

Im creating an e-commerce backend where in each of my product has the following counter attributes

- product views
- product likes
- product comments count

The current database columns which I have for the product database table is

 - id
 - likes_count
 - views_count
 - comments_count
 - category_id
 - category_parent_id
 - category_sub_parent_id
 - handling_charge
 - shipping_charge
 - meetup_address
 - is_additional_fields
 - status
 - is_deleted
 - created_at
 - updated_at

As seen in the following blog Wanelo engineering blog implementing a counter which can frequently get updated on a single row would cause row lock on innodb which if get frequently updated can cause dead lock situations in the application. But the solution for this is pretty much explained in blog which I got an idea about. But what if there are multiple counters associated with a single product which can get updated simultaneously when the app grows. How should I be designing the database table for the counters. Should I have to maintain separate tables viz

likes counter table

 - id     - product_id      - count

views counter table

 - id     - product_id     - count

comments counter table

 - id     - product_id     - count

By maintaining separate table even if a simultaneous update comes for a product (like+comment+view) it will be updated separately and reduces chance of row dead lock situations. If its in a single table and if the updates for all of it comes simultaneously it can cause an issue.

Question: Is there any better way in which I could design the tables for a counter? Any suggestions please?

like image 921
Ajeesh Avatar asked Nov 20 '15 06:11

Ajeesh


1 Answers

A counter in the product table for views is fine.

A separate table for likes with columns like (product_id, user_id) so each user can only like a product once. Otherwise they would be able to mash like if it was just a simple counter.

A separate table for comments with columns such as (product_id, comment_text, date.. etc)

Is this what you're asking?

like image 157
Chris Trudeau Avatar answered Nov 09 '22 11:11

Chris Trudeau