- 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,961Write 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? |