Title 1

Top 10  SOQL Best Practices

LABEL

Title 1

LABEL

Fill in some text

Make SOQL selective by using atleast one query filters on an indexed field. Below fields are indexed field which we should use 1. Primary keys 2. Foreign keys 3. External Ids 4.RecordType fields 5. Audit Fields 6. Unique Id

Title 1

LABEL

Fill in some text

Avoid null values in query condition. Null values will make full table scan which will impact performance.  We can atlease create one default record type and use that in query.

Title 1

LABEL

Fill in some text

Use SOQL For Loop when we need to query and iterate over large data.  SOQL For Loop will fetch a batch of 200 records then iterate over those records one at a time 

Title 1

LABEL

Fill in some text

When we execute a SOQL query inside a FOR loop, the code will make a new call to the Salesforce server for each iteration of the loop. This can lead to performance issues.

Title 1

LABEL

Fill in some text

Limit query result based on requirement. Some time query will return 1000 records and we need only 10 records. It will waste our resources.

Title 1

LABEL

Fill in some text

ORDER BY clause ensures that our results are always sorted in the same way.  This makes it much easier to work with the data because you can predicate which data will return first.

Title 1

LABEL

Fill in some text

add only required fields in  SOQL. Extra fields will take extra time to load and it might some time increase heap size as well.

Title 1

LABEL

Fill in some text

Instead of querying related object seprately make a single query to fetch all related object data.

Title 1

9. Avoid leading – wildcard ('%pen') filters

LABEL

Fill in some text

Avoid leading wildcard (%codex%) filter in SOQL. This will make query non selective and will take too much to query. 

Title 1

10. Avoid negative filters

LABEL

Fill in some text

Avoid negative operators like: Status__c != 'Active' tablescan will be used here.  Instead use: Status__c IN ('Open', 'In-Progress')

Title 1

LABEL

Fill in some text

Consider these SOQL best practices to make your query faster. Thank You

Salesforce Inspector

Fill in some text