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
Eran VizelEran Vizel 

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

Hi,

Lately our Case database seemed to have grown so much, that we started getting the excpetion for every Case update attempt:

caused by: 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)


This is the query that triggers this exception:

Map<Id, Case> map_Cases = new Map<Id, Case>([SELECT Id, Account.Renewal_Date__c FROM Case WHERE AccountId != null and AccountId in :set_AccountIds]);

Is AccountId on Case already indexed? If not, can I ask Salesforce support to do it? Will it resolve the problem? And if not, can you think of any other solution? I don't see how I can optimize this query anymore.

Thank you,
Eran
BalajiRanganathanBalajiRanganathan
You dont need to have AccountId != null in the where condition.

Try with this.
Map<Id, Case> map_Cases = new Map<Id, Case>([SELECT Id, Account.Renewal_Date__c FROM Case WHERE AccountId in :set_AccountIds]);
Eran VizelEran Vizel
AccountId != null wasn't actually there before, I added it because I thought this would resolve the problem. So the problem exists without that too...