- SalesforceChaCha
- Posts
- ๐ Guide to selective SOQL ๐บ
๐ 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:
one of its filters is on an indexed field and
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:
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
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'
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."
and now....Your Daily Memes



What did you think about today's newsletter? |