Hello Kirtti Azad Sahoo
All the retrieve object via association is to connect those two entity attributed. The things which you mentioned will perform as best case when the business model or Logic is satisfied.
For example if u count the total number of employee in a company that gives a single whole number. this will be the best case for 1st option and worst case where you use a follow up sum.
I hope it will be useful
A retrieve followed by an aggregate activity results in one query being done.
If you have the opportunity use a retrieve over association (unless you need specific results or your business logic demands a DB retrieve) and use an aggregate function like Count or Sum.
If the required information isnt in memory anyway a retrieve over association will default to a DB retrieve.
If you want to perform multiple aggregates it, in general, is a good idea to retrieve/aggregate/retrieve/aggregate. As a rule of thumb ofcourse and there are exceptions.
Hello, Kirtti Azad Sahoo
In the case of retrieving the object(s) via association, if the objects or their associations are changed and not committed, the data is not yet available in the database. By retrieving By association, the data that is not committed can be retrieved.
So, I think it would optimize the retrieval process if I retrieve the objects from the database and then go through a count activity before doing a sum activity.