Let's say I have two tables: users
and tweets
.
I want to display a full user profile, and latest tweets by id.
Is it better to fetch them in two separate, possibly concurrent/parallel calls? in JS it would be:
const result = await Promise.all(
query(`SELECT * FROM users WHERE id = 1`),
query(`SELECT * FROM tweets WHERE user_id = 1`)
);
const profile = result[0];
const tweets = result[1];
OR
make a one query, simple join and retrieve user data from let's say first element of the array? e.g.
// pseudo code
const tweets = await query(`
SELECT * FROM users
JOIN tweets
ON users.id = tweets.id
WHERE users.id = 1`)
if (tweets.length > 0) {
const { username, avatar } = tweets[0]; // reading any column to get user data
}
Is second query just wasting bandwidth because the user data is copied over every column?
Somehow it stuck with me that it's bad practice to make multiple sql calls and even in such a simple scenario I don't know whether I am overcomplicating things.