Q.When does we use SOQL and SOSL in SALESFORCE?
Answer:
SOQL:
1. You know in which objects or fields the data resides.
2. You want to retrieve data from a single object or from multiple objects that are related to one another.
3. You want to retrieve data from number, date, or checkbox fields.
4. You want to count the number of records that meet specified criteria.
5. You want to sort results as part of the query.
SOSL:
1. You don't know in which object or field the data resides and you want to find it in the most efficient way possible.
2. You want to retrieve multiple objects and fields efficiently, and the objects may or may not be related to one another.
3. 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.
BOTH SOQL OR SOSL:
1. Both SOSL 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.
2. 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.
3. 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.
4. 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.