This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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