Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Prevent Large Data Queries in Salesforce with Transaction Security Policies
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 16
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Integration List
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    SalesforceCodex
    Home»Salesforce»Apex»The Ultimate Checklist for Efficiently Querying Large Data Sets

    The Ultimate Checklist for Efficiently Querying Large Data Sets

    Dhanik Lal SahniBy Dhanik Lal SahniJuly 29, 2024Updated:July 30, 2024No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Salesforce Large Data Set
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Salesforce data grows with an increase in the number of transactions, customer sales and services records, leads, opportunities, and other data. As data volume increases, queries can take longer time to execute. Large data sets can cause governor limits to be reached more quickly, leading to errors and reduced functionality. Large data sets can result in slower page loads and reports. This post will explain all the ways to query large data sets efficiently.

    1. SOQL for Loops

    Initially, Salesforce Org has very few records so SOQL returns very few records very quickly. As organization grows these records keep increasing with customer data. This can lead to Governer limit error for SOQL query execution. Let us take an example Organization is using a Case object to store all support services.

    List<Case> cases=[Select id, status, subject from Case];
    for (Case cs : cases) {
    // DML code here
    }

    The above query will return fewer records initially but after years of usage of the application, records will exceed 50 thousand. It will start throwing an exception in SOQL itself. If SOQL is executed properly then we will get the issue in the loop.

    To handle SQOL where we need to use a loop to iterate and work on the record, we can use the below approach instead of the above one. The below loop can be used to iterate when local variable assignment or computation is required.

    for (Case c : [Select id, status, subject from Case WHERE subject LIKE 'Inquiry%']) {
    // Your code without DML statements here
    }

    To use a loop to iterate and handle a DML statement, we can use the below code.

    for (List<Case> cases: [Select id, status, subject from Case WHERE subject LIKE 'Inquiry%']) {
    for (Case c : cases) {
    // Your code here
    }
    update cases;
    }

    If we assume that this SOQL can hit the Governer Limit better to use Batch Apex to do such operations. Refer post Optimizing Loop in Apex Code to learn more about how to efficiently use loops.

    2. Use Selective Query

    Selective filters reduce the number of records returned by a query, resulting in faster query execution times. Salesforce can retrieve relevant data more quickly by narrowing the dataset, which improves the application’s overall performance. By limiting the number of records returned, selective filters help to reduce memory consumption and avoid governer limits. This is critical in avoiding heap size limits, which can result in errors and performance degradation.

    Check out the post Optimize SOQL Filter in Apex Code to learn more about selective filters in Salesforce Apex.

    3. Avoid Unwanted Fields in SOQL

    Avoid unwanted fields in SOQL and use only required fields. This will reduce the amount of data retrieved and processed. Sometimes we put unwanted fields and when we do a DML operation, it may fire a trigger that should not fire. This will add extra time for operation and might throw other governer limit exceptions.

    Try to change existing code where unwanted fields are mentioned. If it is hard to change the existing codebase then atleast avoid it in new code.

    4. Use Relationship Query Wisely

    Relationship queries allow us to traverse relationships between objects (for example, fetching Contacts associated with an Account) in a single query. This helps to minimize the query overhead and improve query execution speed.

    We can improve performance by efficiently processing related data in a single query, particularly when dealing with large datasets. It minimizes database round-trips and improves data retrieval, resulting in faster execution and better resource utilization.

    Let us take an example, we want to process an account and its related contacts. The below code uses multiple queries to fetch data.

    We can efficiently query data of accounts and related contacts using the below approach.

    5. Use LIMIT to return the number of records

    Salesforce imposes a variety of governor limits to ensure that resources are used efficiently in a multitenant environment. Querying too many records can exceed these limits, resulting in runtime exceptions. Limiting the number of records returned can improve query performance by reducing processing time and resource usage.

    6. Use Batch Apex

    If we have large no of records to process then we can use Asyncrnouse process like Queueable Apex or Batch Apex. These process can be used in different scenerio. Batch Apex breaks the process into smaller chunks so it can handle large no of records.

    Below batch apex will process all account and its associated contact. System will divide all records in chunks and process each record asynchronously.

    Refer post Avoid Batch Apex and Use Queueable Class to learn more about Batch Apex and Queueable Job.

    7. Use SOSL for Text Searches

    SOSL (Salesforce Object Search Language) should be used to search text in Salesforce Apex for large datasets. It allows for quick full-text searches across multiple objects and fields. SOSL queries can return data from multiple objects and are designed for search operations, making them an effective tool for dealing with large datasets.

    8. Use Custom Indexes

    Custom indexes significantly reduce the time required to search and retrieve records from large datasets. Without indexes, queries might require full table scans, which are resource-intensive and slow. Custom indexes help to ensure that queries are selective. It means they return a smaller subset of records rather than the entire dataset. Salesforce has a threshold for query selectivity, and non-selective queries can cause performance issues. Custom indexes are created by Salesforce support team, so when it is required we need to raise a request with complete detail.

    Checkout our post Optimize SOQL Filter in Apex Code to understand about selective query in Salesforce.

    References

    • Working with Very Large SOQL Queries
    • Make SOQL query selective

    Related Posts

    1. Optimizing Salesforce Apex Code
    2. Optimizing Loop in Apex Code
    3. Handle Heap Size for Apex Code Optimization
    4. Enhance Apex Performance with Platform Caching
    5. Apex Trigger Code Optimization
    6. Optimize Code by Disabling Debug Mod
    7. Optimize Apex Code by Metadata Caching
    apex apex best practices batch apex non selective query salesforce salesforce apex soql SOQL Limit
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUltimate Guide to Integrate Stripe with Salesforce CRM
    Next Article How to Integrate Google reCaptcha v3 into the Salesforce Experience Site
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Prevent Large Data Queries in Salesforce with Transaction Security Policies

    August 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 Salesforce Developer Blog by SalesforceBen.com
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (117) apex best practices (5) apex code best practice (10) apex code optimization (6) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (24) salesforce (151) salesforce apex (53) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) security (4) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Prevent Large Data Queries in Salesforce with Transaction Security Policies
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    Archives
    Categories
    Tags
    apex (117) apex best practices (5) apex code best practice (10) apex code optimization (6) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (24) salesforce (151) salesforce apex (53) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) security (4) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.