Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@TheVishnuKumar
Last active January 1, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheVishnuKumar/6d3fc5c547773e34b46a38f01d1c3be6 to your computer and use it in GitHub Desktop.
Save TheVishnuKumar/6d3fc5c547773e34b46a38f01d1c3be6 to your computer and use it in GitHub Desktop.
/*
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);
}
/*
EDIT: 1 Jan 2019
We can use this to get ids of records instead of using above for loop
Suggested by ifernandosousa
Set<Id> lIds = (new Map(leadLst)).keySet();
*/
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