Hello. Please help me solve this problem:
A person views the news (the id of the news is transmitted via GET), I need to get into the database to select the text of this news from one table, comments to this news from the second table, and information about users who left comments from the third table.
What should I do in terms of query optimization. Should we do three different queries for this, or, for example, should we select the text of the news from one table, and then combine the comment table with the data table about the user who left the comment. Thank you in advance for your response.
Answer 1, authority 100%
Storing usernames in the table with comments will only add additional gimor, so, in my opinion, it will be easier to leave all 3 tables and select records from there using join3 select.
And if we’re going to be distorted by unnecessary optimizations, then it’s worth making a separate comments(longtext) field right in the news table, and storing all comments, user ids and their nicknames in json format, for example, in this field.
So tight that it was worth thinking about optimization? For me, this is the standard model for displaying news with comments.
It all depends on what information about the user you display, if only the name, then in principle you can add another field to the table to the comments – the name of the user who added the comment.
You could end up with two simple database queries. One that displays the news, and the second – which displays the comments (by the news ID passed via _GET) and the username and ID, which can be used to get to the profile.