Understanding about SOQL Query Limits


In Real world programming, we will write SOQL query in different ways. But we need to remember that Our Salesforce runs on "Multi-Tenant Architecture" so for everything we write has limits.

What is the transaction ?? 

Suppose if you are trying to insert 100 records through data loader and you gave batch size as 2 then every 2 records are treated as 1 transaction. In this example, you will get 50 transactions.

Types of governor Limits?

There are 2 types of limits
(a).  Rolling 24-hours limits [Consumed limits will be released only after 24-hrs.]
For example Bulk API, Real-time API (SOAP or REST)

(b).  Transaction limits [On completion of each transaction, limits will rest]
SOQL query limits are transaction limits, for example:
=> In the Synchronous process,  per transaction we will get 100 SOQL queries. 
=> Max no.of returned records =50,000
=> Total query execution time =  2 Minutes (If it is more than this then entire transaction will fail)

What happens if I have many managed packages installed in my Org?

Let's say if you have managed package installed in your Salesforce org and as part of this if any SOQL query runs then you will get additional limits. Yes, true. Per managed package, you will get again 100 SOQL queries. The maximum is 1,100 SOQL queries.

Let's jump into the practical scenarios to understand more about limits consumption :

LIMITS class will give detailed information about limits.

1. Simple SOQL query:

Select Id, Name From Account

system.debug(Limits.getQueries());   => Output : 1

system.debug(Limits.getLimitQueries()); => Output : 100 

Here, 
getQueries() =  SOQL queries consumed.
getLimitQueries() = Total no.of SOQL queries allowed per transaction.

2. Parent-child SOQL Query :

Here is the simple formula : 
If it is Parent-child SOQL query then Limits consumed = [Child queries are consumed against aggregate query limits(300 per transaction)]  +  [Parent Queries are consumed against actual SOQL queries limits (100 Per transaction)]

SOQL Query :

List<Account> accList =[Select Id,Name, (Select Id,name from Contacts), (select id from Cases), (select id from Opportunities), (select id from Assets) From Account];

system.debug(Limits.getQueries());  => Output : 1
system.debug(Limits.getAggregateQueries());  => Output : 4

Here,
List<Contact> accList =[Select Id, Account.Name,Account.site From Contact];
system.debug(Limits.getQueries()); => Output : 1
system.debug(Limits.getAggregateQueries());  => Output : 0
List<Contact> accList =[Select Id,LastName from contact where AccountId in (Select Id from Account)];
system.debug(Limits.getQueries()); => Output : 1
system.debug(Limits.getAggregateQueries());  => Output : 0
SOQL Joins also, consume against SOQL query limit 100.
system.debug(Limits.getQueries()); => Output : 1
system.debug(Limits.getAggregateQueries());  => Output : 0
2. If SOQL query return type is List<AggregateResult> then the limits will change. I will explain those in my next post.

system.debug(Limits.getLimitQueries());  => Output : 100

system.debug(Limits.getLimitAggregateQueries());  
=> Output : 300

getAggregateQueries() = No.of child queries executed.
getLimitAggregateQueries() = Total child queries that are allowed per transaction.

3. Child-Parent SOQL query :
system.debug(Limits.getLimitQueries()); => Output : 100

system.debug(Limits.getLimitAggregateQueries()); 
 => Output : 300

Child to Parent SOQL queries will just consume only against SOQL query limit 100.

4. SOQL Joins:
system.debug(Limits.getLimitQueries()); => Output : 100

system.debug(Limits.getLimitAggregateQueries());  
=> Output : 300
5. SOQL Aggregate Functions:

List<AggregateResult> aggRes = [Select count(Id),Name from Account group by Name having count(Id)>1];

system.debug(Limits.getLimitQueries()); => Output : 100

system.deg(Limits.getLimitAggregateQueries());  
=> Output : 300

Note :  Don't confuse with "AggregateResult" class and "Limits.getAggregateQueries()" these two are different.  


tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments

Post a Comment