5 ms·
>Ecto prevents N+1 queries by default, which I think is clearly better How does it know what associations to eagerly load without knowing about the views? It
by aantix 6y ago
>Ecto prevents N+1 queries by default, which I think is clearly better
How does it know what associations to eagerly load without knowing about the views? It can't eagerly load all of the data on complex objects.
- AlchemistCamp 6y agoIt doesn't eagerly load anything. It preloads exactly what you tell it to. Ecto Repo vs Active Record: https://youtu.be/IFKG4Hgt-zM https://youtu.be/IFKG4Hgt-zM
- aantix 6y agoSo you have to be explicit in what to "preload"? How does that differ from specifying an includes on associations for eager loading? It's not saving you any mental energy; you still have to discern what should and shouldn't be loaded up front.
- dmpk2k 6y agoThe question was regarding N+1. If you're not careful with ActiveRecord, you get that. With Ecto, it's impossible. Ecto has its own drawbacks, but its strong suit is predictability.
- aantix 6y agoN+1 is because lazy loading is by default in ActiveRecord. Because it doesn't know anything about your object graph and what associations are needed, so it just loads data as it's encountered. That's a reasonable default. You add includes as you know more about your data and what associations are needed in specific contexts. Eager loading is an optimization, not a prerequisite.