SOQL and SOSL

GOALBest Practice
Allow indexed searches when SOQL queries with multiple WHERE filters can’t use indexesDecompose the query. If you use two indexed fields joined by an OR in the WHERE clause, your search results can exceed the index threshold. Break the query into two queries and join the results.
Avoid querying on formula fields, which are computed in real timeIf you have to query on formula fields, use formulas. Avoid filtering with formula fields that contain dynamic, non-deterministic references. See Standard and Custom Indexed Fields.
Use the most appropriate language, SOQL or SOSL, for a given searchSee Using SOQL and SOSL.
Execute queries with null values in a WHERE filter for picklists or foreign key fieldsUse values such as NA to replace NULLS options. (See Using SOQL and SOSL.)
Design efficient custom query and search user interfacesUse SOQL and SOSL where appropriate, keep queries focused, and minimize the amount of data being queried or searched. (See Using SOQL and SOSL.)
Build efficient SOQL and SOSL queriesUse filters and specific terms in your queries. For SOQL:
  • Use selective filters, which reduce the number of rows the Query Optimizer has to scan. For example, use filters that reference indexed fields and fields that have a wider range of possible values. If filters aren’t selective, the optimizer doesn’t use indexed columns.
  • When filtering on FirstName and LastName, use the Name field instead. For example, Select id, Email from Lead where Name=’Sam Kennedy’
  • Avoid negative filters. For example, status !=’failed’ or status != NULL
  • Use IN instead of a large list of OR statements. For example, id in (‘001xxx’,‘001xxy’, ‘001xxz’).
  • Avoid cross-object reference formula fields. Don’t filter on these objects. They aren’t indexable.
For SOSL:
  • For SOSL, selective filters reduce the number of irrelevant results. If the filters aren’t selective and there are search terms matches for more than 2,000 records, results can be impacted by search crowding.
  • Don’t index custom objects that you don’t want searched. It adds to the number of records available to search, which can lead to search crowding.
  • Filter out objects you don’t want searched.
  • Use specific search terms.
  • Use targeted search groups. Search groups include NAME, EMAIL, and PHONE fields. For example, FIND ‘Avery Smith’ IN NAME FIELDS RETURNING Account(Id,Name), Lead(Id,Name)
Avoid timeouts on large SOQL queriesTune the SOQL query, reducing query scope, and using selective filters. Consider using Bulk API 2.0 with Bulk API 2.0 Query. If you’ve used the previous suggestions and still get timeouts, consider adding a LIMIT clause (starting with 100,000 records) to your queries. If using batch Apex for your queries, use chaining to get sets of records (using LIMIT) or consider moving filter logic to the execute method.