Sub topic 10
Understanding SOQL governor limits
This chapter assumes you can write an inline query and use bind variables. It also assumes you know that Apex code runs inside a single transaction.
What governor limits are
Salesforce runs many customers on shared infrastructure. To keep one customer's code from using all the resources, the platform enforces governor limits on every transaction. A transaction is one unit of work, such as a trigger firing after a save, or one execute of an Apex class. When code exceeds a limit, the platform throws a System.LimitException and rolls back the whole transaction.
You cannot catch a LimitException with try/catch. You have to write code that stays under the limits.
The limits that affect queries
| Limit | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries issued | 100 | 200 |
| Total rows retrieved by SOQL | 50,000 | 50,000 |
| Heap size | 6 MB | 12 MB |
Asynchronous means Batch Apex, Queueable, and @future code. Check the current Apex Developer Guide for exact values, because Salesforce can change them.
Each of these limits has its own error:
Too many SOQL queries: 101means the transaction ran a 101st query.Too many query rows: 50001means the queries together returned more than 50,000 rows.Apex heap size too largemeans the records you stored in variables used too much memory.
Every query counts against the limit, wherever it appears. A query in a loop counts once per iteration. A query in a helper method called from a trigger counts too. All code in the transaction shares one budget, including triggers, flows, and other classes.
How a loop breaks the limit
A trigger can receive up to 200 records at a time. If the code runs one query per record, a bulk data load gives it 200 queries, and the 101st query fails.
This code works in a one-record test and fails on a bulk load:
for (Contact c : contacts) {
// One query per contact: fails once there are more than 100 contacts
Account a = [SELECT Name FROM Account WHERE Id = :c.AccountId];
}The fix is to collect Ids, run one query with IN, and look the results up in a map. That pattern is covered in the bulk-safe patterns chapter.
Measuring your usage
The Limits class reports how much of each limit the transaction has used so far. It is useful when you debug a limit error.
System.debug('Queries: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
System.debug('Rows: ' + Limits.getQueryRows() + ' of ' + Limits.getLimitQueryRows());
System.debug('Heap: ' + Limits.getHeapSize() + ' of ' + Limits.getLimitHeapSize());
List<Account> accts = [SELECT Id, Name FROM Account LIMIT 200];
System.debug('Queries after: ' + Limits.getQueries());
System.debug('Rows after: ' + Limits.getQueryRows());Run this in Anonymous Apex and open the debug log. The query count rises by 1, and the row count rises by the number of records returned.
Common mistakes
- Putting a query inside a
forloop, or inside a method that a loop calls. - Testing with one record. Always test with 200 records to match a real bulk operation.
- Forgetting that limits are shared. Your code may use 20 queries, but a trigger, a flow, and another package can use the rest in the same transaction.
- Loading a very large result into a list. A SOQL
forloop avoids the heap problem, as a later chapter shows. It does not raise the 50,000-row limit.
Exercise
A trigger runs one SOQL query for each of the 200 records it receives. Which error does it throw, which query causes it, and can you handle it with try/catch?
Show answer
It throws System.LimitException: Too many SOQL queries: 101. The 101st query causes it, because a synchronous transaction allows 100. You cannot catch a LimitException, so the fix is to move the query out of the loop and use one query with an IN filter on a set of Ids.