Salesforce SOQL and SOSL Limits for Search Queries

In Salesforce, we use SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) to query, fetch, and search the data of the Salesforce org. To ensure better performance and avoid errors like hitting governor limits, Salesforce has enforced certain limits on SOQL and SOSL queries.

In this Salesforce tutorial, I will explain the limits of SOQL and SOSL for search queries in Salesforce and how we can follow the best practices to avoid hitting the limits.

SOQL Query Limits in Salesforce

First, we will discuss the limits of the SOQL queries in Salesforce that are used to retrieve data from a single object or related object record.

1. SOQL statements:

The limits for the SOQL statements in Salesforce are as follows.

  • Limit for the maximum length of a SOQL statement: The maximum length of a Salesforce Object Query Language (SOQL) statement is 100,000 characters. If a SOQL statement exceeds this limit, the API will return a MALFORMED_QUERY error.
  • Maximum number of junction IDs: In SOQL, the maximum number of junction IDs is 500 per query. If a query includes more than 500 junction IDs, it fails and returns the MALFORED_QUERY exception.
Malformed Query Limit in Salesforce SOQL

2. SOQL WHERE Clause:

In Salesforce, when we execute the SOQL with a WHERE clause, there is a limit of 4000 characters for each string within a WHERE clause.

The default limit for the string characters in SOQL is 100000 characters.

3. SOQL query results:

  • Maximum Rows returned: The maximum result size is 2000, but this can be smaller if you have selected several fields or included blob fields (e.g., long text area fields, rich text area fields) in your SOQL query. In each REST API call, the maximum length for the combined URI and headers isĀ 16,384 bytes.
  • The availability for SOQL query results is 2 days, including results in nested queries.

4. SOQL query Timeout:

For the maximum runtime of an SOQL query, there is a limit of 32 minutes for executing the operation and processing the results. Still, a query can time out at either the execution or processing stage. Out of the 32 minutes, a query operation has 2 minutes to execute and 30 minutes to process results before a timeout occurs.

5. Maximum Number of SOQL Queries:

In Salesforce, the limit for the maximum SOQL queries in synchronous transactions is 100, while in asynchronous transactions, the limit is 200 queries.

6. Aggregate Query Limit:

In Salesforce SOQL, the aggregate query limit is 50,000 records that can be processed in a single query, meaning that an aggregate query can return a maximum of 50,000 aggregated rows before reaching the query limit; each row returned counts towards this limit, even if it represents aggregated data from multiple records.

SOSL Query Limits in Salesforce

In Salesforce, SOSL is used to perform text searches across multiple objects. Its limits are slightly different from SOQL, which are as follows:

1. The maximum length of SOSL statements:

In Salesforce, the limit for the length of the SOSL statements is 100000, which is the same as the maximum length of SOQL queries. For SOSL statements that exceed this maximum length, the API returns a MALFORMED_SEARCH exception code, and no result rows are returned.