I recall hibernate, it maybe rails, as those are my most used tech, that it can detect n+1, and then just query the whole relation.
Detecting is easy if you’re iterating over a proxy collection