Tutorial: How to load related data efficiently

Hi folks,

I meant to publish this trick since I don’t know when and finally came around to it. If you are curious about how to load related data from PostgreSQL (or MySQL or even SQLite) efficiently this might give you an idea.

Cheers, Max

4 Likes

its not similar to subselect and joins in one query?

and maybe return columns as comma separated list came into my mind

select group_concat(yourColumn, ',') from yourtable

1 Like

Thanks for the idea! group_concat does not exist in PostgreSQL, there it is called string_agg. But the fundamental issue here is that string_agg/group_concat is designed to work with single values (“yourColumn” in your example), not entire records (like the rental records in my example) or complex data structures. This is precisely why the JSON approach is superior for this use case.

1 Like

generally JSON result is a interesting way of having hierarchical data.

2 Likes