For anyone who is curious to know the full answer. I have asked Mendix this question via a support ticket, as I was still unsure whether or not the optimization is done for lists retrieved via association. Their reply was as follows:
It seems that if retrieve by association is selected. the optimization does not take place.
Let's see this example with a Main entity and a Sub entity, where Main is associated one-to-many to Sub. If one Main object is retrieved, and then retrieve by association, in the debugger we see the related Sub entities are retrieved and thus there is no optimization taking place. Please note that if there would be an optimized query, would not be visible, as they are not retrieved, as the Sub list would only be counted (see screenshot below)
They have also adjusted the documentation, s.t. it now says: “a database Retrieve activity is only used once in one Aggregate list activity”.
The retrieve over association should be faster but the same optimization will be done. So I would do the retrieve over the association.
Regards,
Ronald