I'm building a Visualforce page in which I want to display two similar (almost identical) lists of the same sObject -- one for objects with an Open status and one for Closed objects. I can think of two ways to do this in the controller -- From an application performance perspective, which approach is better? (In general, there will be anywhere from 2 to 20 objects returned total by the two queries.)
I know it's always good to reduce the number of database queries, but it's also important to reduce the number of code statements executed!
1. Query Twice
List<Custom_Object__c> closedObj = [SELECT Id, Whatever__c, Status__c, Date_Opened__c
FROM Custom_Object__c
WHERE Status__c == "Closed"
ORDER BY Date_Opened__c DESC];
List<Custom_Object__c> openObj = [SELECT Id, Whatever__c, Status__c
FROM Custom_Object__c
WHERE Status__c <> "Closed"
ORDER BY Date_Opened__c DESC];
2. Query Once and Process in Apex
List<Custom_Object__c> openObj = new List<Custom_Object__c>{};
List<Custom_Object__c> closedObj = new List<Custom_Object__c>{};
for (Custom_Object__c theObj : [SELECT Id, Whatever__c, Status__c
FROM Custom_Object__c
ORDER BY Date_Opened__c DESC]) {
if (theObj.Status__c == "Closed") {
closedObj.add(theObj);
} else {
openObj.add(theObj);
}
}