Loading

SOQL Error System.QueryException: Non-selective query against large object type

Дата публикации: Oct 13, 2022
Описание
If you're running a SOQL query on an object with more than 200,000 records while running triggers, it's possible that you'll receive the "System.QueryException: Non-selective query against large object type." error.
Решение
When running a SOQL query, the Salesforce SOQL query optimizer will evaluate the filters used by the query to determine if there is a selective filter that can be used to drive the query execution. When running triggers, if no selective filter is found the query will be marked as non-selective and the mentioned QueryException will be returned. 

A filter is treated as selective if it's based on an indexed field, and it is heuristically found to match less than 10% of the records.

In some cases, a quick fix may be to enable the External ID flag for a field used in the WHERE clause of the SOQL query. As an example,

SELECT Type FROM Account WHERE Text_Field__c = '<value>'

would use the index on Text_Field__c if this field had its External ID flag enabled and the query optimizer heuristically determined that <value> would match 10% of the records or less.

To enable the External ID flag for a field:
 
1) Navigate to the object's field list:

1.a) In Classic, go to Setup | Create | Objects | <object name> | Fields
1.b) In Lightning Experience, go to Setup | Object Manager | <object name> | Fields & Relationships.

2) Then, click Edit next to the field name enable the External ID checkbox.

NOTE: If your SOQL query continues to be unselective even after enabling the External ID flag for a field used in the WHERE clause, log a case with Support for further assistance. Support will determine if there are any candidates for custom indexing, or if the query would need to be modified to include additional filters.

Additional resources:
Номер статьи базы знаний

000384950

 
Загрузка
Salesforce Help | Article