Two objects Opportunity and Asset. Asset object has a lookup field to Opportunity i.e Asset is the child and Opportunity is the parent.
Now say i need to perform some operation on the Opportunity record based on the values from its related child Asset records.
Note: we can get list of Opportunites OR the list of Assets as input.
one way is to write nested for loop something like (if input is Opportunities)
Approach #1
Integer value;
for(Opportunity op : opptyList){
for(Asset as : op.assetlookup__r){
value = value + as.somefieldvalue;
}
op.updatingfield = value;
}
Other way is (if input is Assets)
Approach #2
for(Asset as : assetList){
// use Map asset id as key and the field value to achieve wt we need to
}
Approach #1 has nested for loop which according to coding principles is a bad design.
Approach #2 is a simple single for loop.
But the number of times both Approach #1 and Approach #2 will run seems to same. SO in these sought of situations does a nested for loop or a single loop both same in terms of performance?
Pardon the clarity on the given examples, they are just to give some idea