Friday, 23 May 2014

SOQL vs SOSL - Which one to use and when?

Use the Salesforce Object Query Language (SOQL) to construct simple but powerful query strings in the queryString parameter in the query() call, in Apex statements, in Visualforce controllers and getter methods, or in the Schema Explorer of the IDE.
Similar to the SELECT command in Structured Query Language (SQL), SOQL allows you to specify the source object (such as Account), a list of fields to retrieve, and conditions for selecting rows in the source object. SOQL uses the SELECT statement combined with filtering statements to return sets of data, which may optionally be ordered.
Use the Salesforce Object Search Language (SOSL) to construct text searches in the search() call, in Apex statements, in Visualforce controllers and getter methods, or the Schema Explorer of the Eclipse Toolkit.
Unlike SOQL, which can only query one object at a time, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.

SOQL and SOSL have different indexes. An index makes it much faster to filter queries.
  • Primary keys (Id, Name and Owner fields)
  • Foreign keys (lookup or master-detail relationship fields)
  • Audit dates (such as LastModifiedDate)
  • Custom fields marked as External ID or Unique.
Fields that can't be indexed in SOQL are:
  • Multi-select picklists
  • Currency fields in a multicurrency organization
  • Long text fields
  • Some formula fields
  • Binary fields (fields of type blob, file, or encrypted text.)
Note that new data types, typically complex ones, may be added to Salesforce and fields of these types may not allow custom indexing.
SOSL indexes are:
This is the one point where my discussion is weak. I simply can't seem to find Salesforce documentation on the SOSL indexes. I know there are standard fields like Name that are indexed, but I can't find the documentation for all of it. If anyone can post a comment and/or edit the post here to include that info, I would really appreciate it.

SOQL and SOSL generally have the same limitations, however according to the Governer Limit documentation:
**Description**                                                        **Limit**
Total number of SOQL queries issued                                    100
Total number of SOQL queries issued for Batch Apex and future methods  200
Total number of records retrieved by SOQL queries                      50,000
Total number of records retrieved by Database.getQueryLocator          10,000
Total number of SOSL queries issued                                    20
Total number of records retrieved by a single SOSL query               200
In addition:
  • If a SOQL query runs more than 120 seconds, the request can be canceled by Salesforce.

SOQL syntax and SOSL syntax differ greatly. For a truly in depth break, please refer to their respective documentation. However, a simple example of each is:
SELECT Id, Name FROM Account WHERE Name = 'Acme'
Return all Accounts where the Name is exactly Acme.
FIND {Joe Smith} IN Name Fields RETURNING lead(name, phone)
Look for the name Joe Smith in the name field of a lead and return the name and phone number.

What does Salesforce recommend?
Finally, let's take a look at what Salesforce says is the best option (page 11):
Use SOQL when
  • You know in which objects or fields the data resides.
  • You want to retrieve data from a single object or from multiple objects that are related to one another.
  • You want to count the number of records that meet specified criteria.
  • You want to sort results as part of the query.
  • You want to retrieve data from number, date, or checkbox fields.
Use SOSL when
  • You don't know in which object or field the data resides and you want to find it in the most efficient way possible.
  • You want to retrieve multiple objects and fields efficiently, and the objects may or may not be related to one another.
  • You want to retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.
Some additional considerations when using SOQL or SOSL:
  • Both SOSL search queries and SOQL WHERE filters can specify text to look for. When a given search can use either language, SOSL is generally faster than SOQL if the search expression uses leading wildcards or a CONTAINS term.
  • In some cases, when multiple WHERE filters are being used in SOQL, indexes cannot be used even though the fields in the WHERE clause may be indexed. In this situation, decompose the single query into multiple queries each with one WHERE filter and then combine the results.
  • Executing a query with a null in a WHERE filter makes it impossible to use indexing. Such queries must scan the entire database to find appropriate records. Design the data model not to rely on nulls as valid field values.
  • If dynamic values are being used for the WHERE field and null values can be passed in, don’t let the query run to determine there are no records; instead check for the nulls and avoid the query if necessary.
When designing custom query/search user interfaces, it is important to follow these guidelines:
  • Keep the numbers of fields/searched to a minimum. In LDV environments, querying a large number of fields in the same query can be difficult to performance tune.
  • Determine whether SOQL or SOSL or a combination is appropriate for the search.


Why to use Database.Stateful()?

Database Stateful is interface. It is used in batch classes to maintain the state of the class across transactions i.e. if you want to use ...