๐Ÿ’ƒ Guide to selective SOQL ๐Ÿ•บ

Boost your Salesforce query performance ๐Ÿš€๐Ÿš€

Good morning, Salesforce Nerds! How many times have you heard users complain about waiting for data to load? Reports, list views, Apex taking forever? โŒ›๏ธ 

Many times this can be the result of a poorly written query. ๐Ÿซ  But, how do you know?

Luckily, Salesforce provides a cheat sheet for us to ensure weโ€™re optimizing ๐Ÿ”ฅ our interactions with the database!

Letโ€™s nerd out over this and check out how to make sure our queries are FAST! ๐Ÿ’จ 

Agenda for today includes

  • Guide to selective SOQL

  • Daily Principle

  • All the Memes

Guide to selective SOQL

First, we need some context. Just what does โ€œselectiveโ€ mean? ๐Ÿค” 

In short, a query is considered selective if:

  1. one of its filters is on an indexed field and 

  2. this filter reduces the number of records returned to less than the system-defined threshold

โ—๏ธโ—๏ธโ—๏ธ This is an important concept to understand. โ—๏ธโ—๏ธโ—๏ธ 

If the queries running against your org arenโ€™t selective then this means theyโ€™re performing full table scans. Donโ€™t be that person.

Now we know we want to include indexed fields in our WHERE clauses we need to understand how to find these. ๐Ÿ‘€ 

By default, Salesforce indexes the following fields for you on each Object:

  • Primary keys (ID, Name, and Owner fields).

  • Foreign keys (lookup or master-detail relationship fields).

  • Audit dates (such as SystemModStamp).

  • Custom fields marked as External ID or Unique.

If you have the option, include at least one of these in your queries! The mothership ๐Ÿ›ธ can also add custom indexes for you if you need one.

๐Ÿ‰ HALT! THERE BE DRAGONS HERE ๐Ÿ‰ 

Using an indexed field satisfies the 1st criteria of selective queries. Be aware that there are exceptions thatโ€™ll cause them to become non-selective:

  • Using negative operator (! = and NOT) โ›”๏ธ 

  • Comparing with an empty or null value โ›”๏ธ

  • Using Comparison operators (>, <, >=, <=) with text-based fields โ›”๏ธ

  • Using a LIKE condition with a leading %wildcard โ›”๏ธ

This is step โ˜๏ธ towards a selective query!

The last thing to do is determine if the indexed filters in your query return less than the system-defined threshold. ๐Ÿ˜• 

Itโ€™s a simple formula really:

Standard Indexes:

โœ… 30% of the first 1MM records

โœ… 15% after the first 1MM records

Custom Indexes:

โœ… 10% of the first 1MM records

โœ… 5% after the first 1MM records

If the filter does not exceed these thresholds, then itโ€™s selective and youโ€™re good to go! ๐Ÿ’ฏ 

Letโ€™s see an example lifted straight off the official Salesforce article! Just follow these steps to determine the selectivity of your queries:

  1. Check the total number of records in your Object โœ”๏ธ 

SELECT count() FROM Appointment__c

# of records: 239,619

Threshold for standard indexes: 30% x 239,619 = 71,885
Threshold for custom indexes: 10% x 239,619 = 23,961
  1. Write your SOQL query and replace the list of fields with the count() operator โœ”๏ธ 

SELECT count() 
FROM Appointment__c 
WHERE CreatedDate = THIS_YEAR
AND Status__c = 'Available'
  1. The only filter in the WHERE clause with an index is CreatedDate, so we will only consider the filter on this field โœ”๏ธ 

Partial SOQL query: 

SELECT count() 
FROM Appointment__c 
WHERE CreatedDate = THIS_YEAR

# of records: 84,567 

We can see the amount of records returned here is more than the system-defined threshold of 30% so this query would not be considered selective by the platform.

The next time youโ€™re building a SOQL query, report, or list view remember these steps and your user base will thank you!

Daily Principle

"Everything we hear is an opinion, not a fact. Everything we see is a perspective, not the truth."

Marcus Aurelius

and now....Your Daily Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.