Saving comments for different types of content

I have different types of content on my site. FE - articles, questions, messages I see two ways to store comments.

1) create a table for each content type, for example articles_comments, post_comments, etc.

2) create one table with the fields parent_field and parent_id. in 'parent_field' save smth, for example articles_id or posts_id. therefore with

explode('_', $parent_field); 

I can get the parent table name and field name for

 select "... FROM $table WHERE $field = $parent_id" 

the second method seems more flexible, but at first it’s easier.

So the question is, which way is better, in the case of 100 + k hosts every day?

+4
source share
1 answer

one table for comments only

 id, caption, body, ... 

one relationship table for storing the parent relation to the comment

 comment_id (int), parent_id (int), parent_type (enum, set, or int BUT not varchar) // build index 

that you can easily tag a comment to multiple parents (if you want),
and can easily do an INNER JOIN to get comments on the parent

another advantage
for example, if you want to get the most comments for all articles, questions, and posts,
in your method
you will need three INNER JOIN + GROUP BY,
with my suggestion, you only need one request

the most important thing,
relationship table can handle a huge number of records
because it stores an integer (smaller index)

+1
source

Source: https://habr.com/ru/post/1383043/


All Articles