#Best Practices for Fetching Related Data

1 messages · Page 1 of 1 (latest)

sterile dust
#

I have a specification that fetches related data for the Lead entity in a single query using join fetching. Here is the code snippet:

root.fetch("branch", JoinType.LEFT);
root.fetch("creator", JoinType.LEFT);
root.fetch("resource", JoinType.LEFT);
root.fetch("sales", JoinType.LEFT);

I need to fetch all this related data for each lead. Is it better to fetch all related data in a single query as shown above, or to fetch each relationship in multiple queries? What are the best practices and considerations for each approach in terms of performance, maintainability, and data consistency?

vapid lanternBOT
#

<@&987246584574140416> please have a look, thanks.

untold zealot
#

the less queries, the better

#

but you need to make sure that you don't fetch rows that you don't need

#

if you use directed joins (left/right) that can sometimes be the case

lime inlet
#

Do balance out the number of queries versus whether you need all fields and rows immediately.