Hello friends, today we are going to discuss Boost Your Salesforce Query Performance with @ReadOnly for Large Data. Understanding the @ReadOnly Annotation in Apex. Salesforce’s Apex programming language offers various annotations to enhance the functionality and performance of your code. One such powerful annotation is @ReadOnly.
What is the @ReadOnly Annotation?
The @ReadOnly annotation allows you to perform less restrictive queries against the Salesforce database by increasing the limit of the number of returned rows for a request to 1,000,000. This is particularly useful when dealing with large datasets that exceed the standard SOQL query limit of 50,000 rows.
Key Features and Restrictions
While the @ReadOnly annotation relaxes the query row limit, it comes with certain restrictions to ensure system stability and performance:
- No DML Operations: You cannot perform any Data Manipulation Language (DML) operations such as insert, update, delete, or undelete within a @ReadOnly.
- No Asynchronous Jobs: Calls to System. schedule and enqueued asynchronous Apex jobs are blocked
- Limited to Specific Contexts: The annotation is available for REST and SOAP Web services and the Schedulable interface.
- Increased Query Limit: Allows querying up to 1,000,000 rows, bypassing the standard SOQL limit of 50,000 rows.
- No DML Operations: Prevents any insert, update, delete, or undelete operations within the annotated method.
- No Asynchronous Jobs: Blocks calls to the System. schedule and enqueued asynchronous Apex jobs.
- Specific Contexts: Available for REST and SOAP Web services and the Schedulable interface.
When to Use @ReadOnly
The @ReadOnly annotation is ideal for scenarios where you need to query large volumes of data without modifying it.
- Reporting and Analytics: When generating reports that require querying large datasets.
- Data Export: When exporting data for external processing or backup.
- Read-Only Dashboards: Dashboards that display large amounts of data without the need for user interaction or data modification.
Also, check this: Override Standard Buttons Using Aura
Code :
public with sharing class LargeDataQueryController { @AuraEnabled @ReadOnly public static List<Account> getAccounts() { // This query will be read-only and optimized for performance return [SELECT Id, Name, Industry FROM Account WHERE CreatedDate >= LAST_YEAR]; } }