I’m using JOIN with MySQL to select an article and all comments related to it, but I realized that when I select an article and there’s more than one comment for it, I get the same article duplicated by the number of comments. For example:
I have the table articles with the following columns:
1. id
2. article
And the table comments with the following columns:
1. c_id
2. body
3. a_id
MySQL query I tried to use:
SELECT * FROM `articles`
JOIN`comments`
ON (`articles`.id = `comments`.a_id)
WHERE `articles`.id = 134
And this is the final result:
id article c_id body a_id
134 Article1 2 Comment1 134
134 Article1 3 Comment2 134
134 Article1 8 Comment3 134
So my question is: Since I’m getting the same article column’s value for every comment,
does this slows the MySQL query or my web application by anyway? since I’m requesting more data then what I really need.
I can separate the query to 2 simple SELECT queries, but this will be overkill, right?
Thanks for reading.
Edit:
What if I the article column’s value is bigger, something that contains 1k+ characters, will it make since to duplicate it?
Since the database is transmitting more bytes than it needs to there is performance cost associated with this but it may not be noticeable.
Its hard to say without measuring. If the value of article was a Gigabyte instead of a few characters then you would probably want to do two selects instead of one.