Measure governor headroom
Governor limits cap what one transaction can use, such as 100 SOQL queries.
Your task
Measure how many SOQL queries the loop costs, then load the same Profiles with one query and measure that too.
List<Profile> picked = [SELECT Id FROM Profile ORDER BY Name LIMIT 3];
Set<Id> profileIds = new Map<Id, Profile>(picked).keySet();
Integer loopStart;
List<String> loopNames = new List<String>();
for (Profile item : picked) {
Profile row = [SELECT Name FROM Profile WHERE Id = :item.Id];
loopNames.add(row.Name);
}
List<String> bulkNames = new List<String>();
Integer loopCost = -1;
Integer bulkCost = -1;
System.debug('Loop cost: ' + loopCost);
System.debug('Bulk cost: ' + bulkCost);Requirements
- Reads the query counter should be present
- Uses one IN query should be present
- Measures bulk cost from Limits should be present
- Measures the loop should be printed as
Loop cost: 3 - Measures the bulk query should be printed as
Bulk cost: 1 - Finds the same Profiles should be printed as
Same names: true - Counts five queries in all should be printed as
Total queries: 5