This is the classic N+1, and the standard fix is a DataLoader — it batches all the lookups made within one tick into a single query:
51 queries becomes 2. Two rules that people trip over:
- Create the loader per request, not per process, or users will see each other's cached data.
- The batch function must return results in the same order as the input ids, including
nullfor misses.