Sub topic 16
Query selectivity and performance
This chapter assumes you can write inline SOQL with WHERE filters and bind variables, and that you know the bulk-safe patterns from the earlier chapters. Here you learn why a query that is correct can still be slow or fail on a large object.
What selectivity means
A filter is selective when it narrows a large object down to a small share of its records. Salesforce uses indexes to find matching rows. If a query's filters can't use an index, or would match too many rows, the platform scans the whole object. That is slow, and on large objects it can fail with System.QueryException: Non-selective query against large object type. Objects with more than about 200,000 records are most at risk. The error is most common in triggers.
An index helps only when the filter matches fewer rows than a threshold:
| Index type | Selective if the filter returns fewer than |
|---|---|
| Standard index | 30% of the first million records, then 15% of the rest, up to 1 million rows |
| Custom index | 10% of the first million records, then 5% of the rest, up to 333,333 rows |
Standard indexes exist on fields such as Id, Name, OwnerId, CreatedDate, SystemModstamp, lookup and master-detail fields, and unique or external ID fields. Salesforce Support can add custom indexes on other fields.
Filters that defeat an index
- A leading wildcard, such as
LIKE '%corp'. - Negative operators, such as
!=andNOT IN. - Comparing a field to
null. - Formula fields that can't be indexed.
- Filtering on a field that is not indexed at all.
// Likely non-selective on a large object
List<Account> slow = [
SELECT Id, Name FROM Account
WHERE Name LIKE '%corp' AND Industry != 'Energy'
];
// Selective: indexed fields, a bounded date range, and a limit
Id ownerId = UserInfo.getUserId();
List<Account> faster = [
SELECT Id, Name
FROM Account
WHERE OwnerId = :ownerId AND CreatedDate = LAST_N_DAYS:30
LIMIT 200
];Checking a query
Use the Query Plan tool in the Developer Console. Enable it under Help > Preferences > Enable Query Plan, then paste your SOQL into the Query Editor and click Query Plan. It lists each way Salesforce could run the query and a cost for each. A cost below 1 means the plan uses an index selectively. The lowest-cost plan is the one Salesforce prefers.
Practical habits
- Filter on indexed fields first, and combine them with
ANDto shrink the result. - Select only the fields you need. Fewer fields means less heap used.
- Add
LIMITwhen you only need a few rows. - Avoid filtering on
nulland avoid leading wildcards. - Keep queries out of loops, as in the bulk-safe chapter. One query that is selective beats many that are not.
Exercise
A trigger queries SELECT Id FROM Case WHERE Status != 'Closed' on an org with 3 million cases and fails with a non-selective query error. Name the problem and one fix.
Show answer
The negative operator != on Status can't use an index efficiently, and most cases are likely to match. Add a selective indexed filter such as OwnerId, AccountId, or a CreatedDate range, or have Salesforce Support index a field that narrows the result. Confirm the fix in the Query Plan tool.