Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Speed Test - Filtering records using List<Id> versus List<sObject> in SOQL
Author: 0to1Code.Com
*/
public class ApexSpeedExperiment_2{
//Experiment 1 : Querying records using list of sObject with Id fields
public static void runExperiment1(){
//This Query only includes only Id field
List<Lead> leadLst = [select id from lead];
system.debug('CPU Limit Consumption Start: '+Limits.getCPUtime());
List<Lead> reQueryingLeads = [select id,name,company from lead where id in: leadLst];
system.debug('CPU Limit Consumption End: '+Limits.getCPUtime());
}
//Experiment 2 : Querying records using list of Ids
public static void runExperiment2(){
List<Lead> leadLst = [select id from lead];
List<Id> lIds = new List<Id>();
for( lead l : leadLst){
lIds.add(l.id);
}
system.debug('CPU Limit Consumption Start: '+Limits.getCPUtime());
List<Lead> reQueryingLeads = [select id,name,company from lead where id in: lIds];
system.debug('CPU Limit Consumption End: '+Limits.getCPUtime());
}
//Experiment 3 : Querying records using list of sObject with more fields
public static void runExperiment3(){
//This Query only includes more than one field
List<Lead> leadLst = [select id,name,company, Address, Industry, AnnualRevenue from lead];
system.debug('CPU Limit Consumption Start: '+Limits.getCPUtime());
List<Lead> reQueryingLeads = [select id,name,company from lead where id in: leadLst];
system.debug('CPU Limit Consumption End: '+Limits.getCPUtime());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment