I am getting the following error for a query I am running dynamically

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)

The query is

SELECT Prior_Parent_Id__c, COUNT(Id) FROM Preferences__c WHERE Prior_Parent_Id__c in :originalIds GROUP BY Prior_Parent_Id__c

The originalIds list is populated and does not contain a null. What is more making this weird is that there are 0 records in the sandbox I am running this in for that object type.

I have raised a case with SFDC but wondered if anybody had any ideas why a query on an object with 0 rows returns such an exception.


I worked with Salesforce support on this one for a while but to no avail. The issue seemed to disappear after a week and we were unable to replicate it again. We performed an interim update changing the fields to external Ids to aid in resetting the indexes and counts for the queries.

  • Out of interest is originalIds a list or a set ? Commented Jan 30, 2015 at 2:48
  • A list. However the same list is being used in a number of different dynamic queries of the same format in the same method - just a different object - which are running fine.
    – pbattisson
    Commented Jan 30, 2015 at 5:31
  • @pbattisson Did the support case reveal anything? I've got a similar issue in salesforce.stackexchange.com/q/71609/102 Commented Apr 9, 2015 at 1:34

1 Answer 1


I encountered a very similar issue with a custom sandbox sObject that had no records but was failing when queried in a trigger context.

In my case waiting for a period of time allowed Salesforce to re-evaluate the sObject cardinality. It went from 7869700 to 1 overnight. As a result the cost of the table scan went from 5.02 to 0.65, enough to be consider selective.

Did your problem also resolve itself in time?

  • Marking this as the answer as it best describes how mine resolved itself - wait and it self-resolved.
    – pbattisson
    Commented Apr 10, 2015 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .