I'm working on a review system and want to add likes and dislikes functionality. Is it better to create a separate table to store each reaction, meaning each like or dislike would take up one row? For example, if one user likes 5 reviews and there are 20 users, that would mean 100 rows for a single post! Also, do I need to update the database every time someone reacts, or is there a more efficient approach?
5 Answers
A practical tip: you could keep a count of likes and dislikes directly in the posts table for quick access, while maintaining a separate reactions table for detailed relationships. Just ensure you handle the consistency between these two data sources properly to avoid discrepancies!
Here's a practical way to set it up: Create a 'Reactions' table with columns for review_id, user_id, and reaction_type (like, dislike). This setup will support future reaction types too! Make sure to set a composite primary key on review_id and user_id to ensure each user can only react once per review. Essentially, this gives you a clean way to manage user reactions without cluttering other tables.
Yeah, you should definitely create a separate table for handling reactions. This way, you can easily track whether a user liked or disliked a specific review. Each reaction should be a separate record to allow for easy updates or removals later. For the user interactions, you’ll need to insert or delete rows based on their actions, but you don’t necessarily have to keep a total count in the main table since you can derive that from the reactions table itself.
Also, consider implementing a unique index for user ID and review ID. It speeds up lookups and prevents a user from reacting multiple times to the same review.
Keep in mind the scale of your application. If you're expecting high traffic, think about optimizing for performance—possibly by caching totals or using batching for DB updates. Also, consider implementing a queue for handling asynchronous updates to reduce load during high interaction times.
Exactly! Optimistic UI updates might also help users see their interactions without delays, improving user experience overall.
If you need detailed interaction tracking, a row per interaction is the way to go. Alternatively, using a JSON column to hold all user interactions can be beneficial but comes with parsing challenges. Popular posts can also bloat this JSON data, making performance a concern.

That's a solid suggestion! In fact, platforms like Instagram used to manage reactions this way before shifting to a mixed model that tracks overall likes separately while keeping individual reactions in a dedicated table.