Skip to content

SOQL Performance Optimization: Enhancing Query Efficiency

Published 16/08/2025

SOQL Performance Optimization Hero Image

In the fast-paced world of Salesforce, optimizing SOQL queries is crucial for ensuring efficient data retrieval and maintaining the overall performance of your Salesforce applications. Poorly optimized queries can lead to slow response times and increased resource consumption, impacting user experience and system performance. In this post, we’ll discuss techniques for optimizing SOQL queries, including using selective filters, understanding query plans, and implementing indexing strategies.

Selective filters are essential for efficiently narrowing down the dataset returned by a query, reducing the amount of data processed and improving performance.

Selectivity: A filter is considered selective if it significantly reduces the number of records returned compared to the total number of records in the object. Aim for filters that return less than 10% of the total records.

Best Practices:

  • Use indexed fields in your WHERE clause to improve selectivity.
  • Combine multiple filters to create a more selective query.
  • Avoid using negative filters (e.g., != or NOT IN) as they are less selective.

Query plans provide insights into how Salesforce executes your SOQL queries, helping you identify potential performance bottlenecks.

Query Plan tool: Use the Query Plan tool in Salesforce Developer Console to analyze and optimize your queries. It provides insights on the cost of executing a query and whether it uses indexes, helping you identify and address performance bottlenecks.

  • Check the Query Plan for slow-executing SOQL queries.
  • Gain insights into different execution plans and compare the cost of using indexes versus full table scans.
  • If a table scan is cheaper than using an index, consider using other filters to improve selectivity or indexing a non-indexed selective filter.
  1. In the Developer Console, click Help | Preferences.
  2. Set ‘Enable Query Plan’ to TRUE.
  3. Access the Query Plan Tool in the ‘Query Editor’ tab of the console.
  4. Enter a query in the Query Editor and press Query Plan to display all query operations and their costs.

Index Presence: Check if the filter has an index.

Selectivity Thresholds:

  • Standard index: 30% of the first million records, 15% after that.
  • Custom index: 10% of the first million records, 5% after that.

If a filter exceeds the threshold, it won’t be considered selective.

Query Plan Screenshot

Query Plan Screenshot

  • Cardinality: Estimated number of records returned by the leading operation type.
  • Fields: Indexed fields used by the Query Optimizer.
  • Leading Operation Type: Primary operation type (Index, Sharing, TableScan, Other).
  • Cost: Derived from database statistics; values above 1 indicate non-selectivity.
  • sObject Cardinality and Type: Approximate record count and name of the queried object.
  • Each plan has a cost value based on database statistics. The plan with the lowest cost is used.
  • A cost above 1 indicates non-selectivity.
  • Indexed fields appear in plans only if supported operations are used.
  • Unsupported operations include “NOT EQUAL TO”, comparisons with null values, leading ‘%’ wildcards, and non-indexed OR comparisons.
  • Review the query plan to ensure your query is using indexes effectively.
  • Look for high-cost operations and adjust your query to reduce them.
  • Use the query plan to identify and eliminate full table scans.

Indexes are critical for improving query performance by allowing Salesforce to quickly locate and retrieve records.

Standard and Custom Indexes: Salesforce automatically indexes certain fields, such as primary keys (Id, Name, OwnerId) and foreign keys (CreatedById, LastModifiedById, lookup, master-detail relationship). You can also add indexes to custom fields by marking them as Unique or External Id or requesting indexing for fields frequently used in queries.

Best Practices:

  • Use indexed fields in your WHERE clause to speed up query execution.
  • Limit the number of fields in your query to those necessary for your use case.
  • Regularly review and update your indexing strategy based on query performance.

The structure of your query can significantly impact its performance.

Limit Returned Fields: Only select the fields you need to minimize data transfer and processing time.

Use LIMIT and OFFSET: When dealing with large datasets, use the LIMIT and OFFSET clauses to paginate results and reduce the load on the system.

Avoid Complex Joins: Simplify your queries by minimizing the number of joins and subqueries, which can increase complexity and execution time.

Regularly monitoring and analyzing query performance is essential for maintaining optimal system performance.

Use Salesforce Tools: Leverage Salesforce’s built-in tools, such as the Developer Console and Workbench, to monitor query performance and identify areas for improvement.

Best Practices:

  • Set up alerts for long-running queries to proactively address performance issues.
  • Continuously review and refine your queries based on performance data.

Optimizing SOQL queries is a vital skill for any Salesforce developer or administrator. By implementing selective filters, understanding query plans, and leveraging indexing strategies, you can enhance the performance of your queries and ensure a smooth user experience. Regularly monitoring and analyzing query performance will help you maintain optimal system efficiency and support your organization’s data-driven goals.

I hope this guide has provided you with valuable insights into optimizing your SOQL queries.