function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

System.QueryException: Non-selective query against large object type (more than 100000 rows).

I am getting following error,
FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id a00A0000005jZMDIA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,  reciprocalRelationship: execution of AfterUpdate

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

My SOQL query is,

SELECT  ReciprocalType__c, ReciprocalId__c, Notes__c, End_Date__c FROM Realted_Obj__c WHERE isDeleted = false and ((Account2__c  != null and Account__c  != null) OR (Contact__c  != null and Contact_2__c  != null)) and ReciprocalId__c IN: setReciprocalId

setReciprocalId : this set doent contain any null value.

Also in this object Realted_Obj__c , more than 4,00,000 records are there.

Any suggestions?


You need to make sure that all fields mentioned in WHERE are indexed

For example, I assume that Account__c is a lookup to Account object, so this one shall be fine because lookups are usually(*) indexed by default.

What about: Realted_Obj__c.ReciprocalId__c
What type is it?
If it is a text field then can you make it "External Id" to enforce indexing?

Or alternatively contact SFDC support to see if they can index necessary fields for you.

(*) it turns out that not all lookups are indexed, there used to be some sort of a bug in SFDC related to that, not sure if it is fixed yet.