When extracting data from multiple tables for mapping into a GraphQL result, which is more effective?
Pseudocode below.
Version 1 - Joining N tables together and parsing them into separate objects on the caller's side
alldata = SELECT a.*, b.*, c.*
FROM aaaa a
LEFT OUTER JOIN bbbb b on a.id = b.parent_id
LEFT OUTER JOIN cccc c on b.id = c.parent_id
WHERE a.name = 'my name';
/* Map flat DB rows to Java pojo-s */
aObj = parseRowsIntoJavaObjects(alldata)
Version 2 - Performing selects on tables 1 by 1, limiting them by parent row id-s
a_rows = SELECT a.* FROM aaaa a WHERE a.name = 'my name';
b_rows = SELECT b.* FROM bbbb b WHERE b.parent_id IN (a_rows.id);
c_rows = SELECT c.* FROM cccc c WHERE c.parent_id IN (b_rows.id);
/* Map flat DB rows to Java pojo-s */
bObj = parseRowsIntoJavaObjects(b_rows, c_rows);
aObj = parseRowsIntoJavaObjects(a_rows, bObj);
The tables are connected using foreign keys and can feature 1:1, 1:N and N:N relationships. Their structure is otherwise not defined - the solution has to work against arbitrary datamodels. Indexes can be added as needed and it can be assumbed that any necessary indexes are present for the query execution.
Asking because there can be any number of tables joined in this manner and I am concerned if Version 1 causes a problem for having to return the same data multiple times in several rows due to how many-to-one relationships work.
I know making several queries is generally a bad idea if things can be managed with one and IN() is not particularly fast. Is using IN here still a bad idea?
Since the solution has to be generic and apply to any number of datamodels, I don't want to just try it out and optimize it for a specific model like most questions dealing with JOIN... WHERE IN().. do. I would like to ask for a suggestion of what is the generally more performant way of loading relational data from arbitrary hierarchies up to.. say 3 levels deep?
The only sort-of matching question I found on SO dealt with a slightly different case but advocated both somewhat similar solutions in its answers: Select from multiple tables - One to Many relation
Database: MariaDB