I have a very simple
after delete trigger that searches for orphan records and deletes them:
List<my_ns__Orphanable_Object__c> orphans = Database.query( ' SELECT ' + ' Id ' + ' FROM ' + ' my_ns__Orphanable_Object__c ' + ' WHERE ' + ' my_ns__Lookup1__c = null ' + ' AND my_ns__Lookup2__c = null ' + ' AND my_ns__Lookup3__c = null ');
One of our customers has a large amount of data in the "Orphanable Object" and recieves this error when deleting a record:
System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com 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)
What I can not understand is; why does this only happen in the trigger context?
To debug the issue I ran the same code in the devloper console and it ran without issue and the
orphans list contained ~250 records. This also removed the possibitlity that the query was returning too many results.