Performance
SOQL Governor Limits
Understanding Governor Limits
SOQL governor limits restrict query rows and execution time.
What Are SOQL Governor Limits?
SOQL (Salesforce Object Query Language) governor limits are rules set by Salesforce to ensure efficient use of system resources. These limits are crucial for maintaining the stability and performance of the Salesforce platform, especially in multi-tenant environments. Governor limits enforce restrictions on the number of records that can be retrieved by a single query and the total execution time of queries.
SOQL Query Row Limits
One of the key SOQL governor limits is the restriction on the number of query rows. Each query can return a maximum of 50,000 records. If a query attempts to return more than this number, a runtime exception will occur. It's important to design queries to work within these limits to avoid exceptions.
SOQL Execution Time Limits
Another critical limit is the total time allocated for SOQL queries within a transaction. The maximum allowed time for executing all SOQL queries within a transaction is 100 seconds. This limit ensures that no single transaction monopolizes the shared resources, allowing Salesforce to serve multiple users efficiently.
Best Practices to Avoid SOQL Limit Errors
- Use SOQL for loops: Use SOQL for loops to handle large datasets efficiently without hitting governor limits.
- Filter data effectively: Apply filters to reduce the number of records returned by the query.
- Index fields: Ensure that fields used in WHERE clauses are indexed to improve query performance.
- Use selective queries: Write queries that are selective and avoid using broad queries that return large datasets.
Handling SOQL Governor Limit Exceptions
When a SOQL query exceeds governor limits, Salesforce throws a TooManyRowsException
or CpuLimitExceededException
. To handle these exceptions, utilize try-catch blocks and ensure that your code gracefully manages these errors to provide a smooth user experience.
Performance
- Query Limits
- Selective Queries
- Query Optimization
- Bulk Queries
- Governor Limits
- Previous
- Bulk Queries
- Next
- Injection