In the previous chapters, you’ve built a strong foundation in SOQL, learned how to craft basic and intermediate queries, and explored powerful techniques like aggregate functions and relationship queries. Now, it’s time to take your skills to the next level with advanced query optimization. In this chapter, we’ll delve into strategies for querying large data sets efficiently, bulkifying SOQL queries, understanding query execution plans, implementing indexing best practices, and optimizing query selectivity.

Querying Large Data Sets

As your Salesforce database grows, it becomes crucial to retrieve data efficiently, especially when dealing with large volumes of records. Efficiently querying large data sets not only ensures faster response times but also minimizes the impact on system resources. Here are some best practices for querying large data sets:

  1. Use SELECTive Filters: Craft your queries with selective filters that limit the number of records returned. Highly selective filters reduce the amount of data processed and improve query performance.
  2. Leverage INDEXes: Take advantage of custom indexes to speed up data retrieval. Salesforce automatically creates indexes for certain standard fields, but you can also create custom indexes on frequently queried fields.
  3. Implement Pagination: When querying large data sets, use pagination techniques to retrieve records in smaller chunks. This prevents timeouts and ensures a smoother user experience.

Bulkifying SOQL Queries

In Salesforce, it’s essential to write code that can handle data in bulk. Bulkifying SOQL queries ensures that your code can efficiently process multiple records simultaneously. For example, if you’re working with triggers or batch processes, bulkification becomes critical to prevent hitting governor limits. Here’s an example of bulkifying a query:

In this code snippet, accountIds is a collection of Account IDs, and the query retrieves corresponding Contact records efficiently in bulk.

Understanding Query Execution Plans

A query execution plan outlines the steps the database engine takes to process your query. Understanding query execution plans helps you identify potential performance bottlenecks and optimize your queries accordingly. Salesforce provides an “Query Plan” feature to analyze query execution plans. Here’s how you can use it:

  1. Open Developer Console: Navigate to the Developer Console in Salesforce.
  2. Execute Query: write your SOQL query.
  3. Click on “Query Plan”

Analyzing the execution plan helps you determine whether indexes are being used effectively, which can guide your optimization efforts.

Indexing Best Practices

Indexes play a crucial role in query optimization. They enhance data retrieval speed by allowing the database engine to quickly locate records. Salesforce automatically creates indexes for certain fields, but you can also create custom indexes to optimize specific queries. Consider the following indexing best practices:

  1. Selective Indexes: Create custom indexes on fields that are frequently used in WHERE clauses or for filtering data. Ensure that these indexes are selective to maximize performance gains.
  2. Compound Indexes: Use compound indexes for queries with multiple filter conditions. Compound indexes cover more query scenarios and can improve performance significantly.
  3. Index on Foreign Keys: Index foreign key fields used in relationship queries to improve the speed of parent-child or child-parent queries.

Optimizing Query Selectivity

Query selectivity refers to the ratio of selected records to the total number of records in a queried object. Optimizing query selectivity is essential to ensure efficient data retrieval and query performance. Salesforce has a query optimizer that estimates query selectivity and determines the most efficient way to process your query. However, you can take steps to optimize selectivity:

  1. Selective WHERE Clauses: Craft WHERE clauses that are highly selective and use indexed fields. This reduces the number of records retrieved and enhances performance.
  2. Use of Filter Logic: Employ filter logic (AND, OR) in your queries strategically to improve selectivity. Consider whether using multiple queries with fewer filter conditions might be more efficient.
  3. Bulk Data Considerations: When writing code, ensure that it can handle both single-record and bulk data processing to accommodate various scenarios.


Congratulations! You’ve delved into the world of advanced query optimization in SOQL. By mastering these techniques, you’re equipped to tackle even the most complex data retrieval challenges efficiently and effectively. Querying large data sets, bulkifying queries, understanding query execution plans, implementing indexing best practices, and optimizing query selectivity are essential skills that will empower you to make the most of your Salesforce data.

As we continue our journey through this guide, you’ll be well-prepared to handle real-world scenarios, craft optimal queries, and extract valuable insights from your Salesforce environment. In the next chapter, we’ll dive into data manipulation with SOQL, where you’ll learn how to insert, update, delete records, manage transactions, and perform upsert operations. Get ready to take your data manipulation skills to the next level!

About the blog

SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:


Subscribe to our email newsletter to be notified when a new post is published.