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
Kevin DesaiKevin Desai 

Error : Non Selective Query on Contact

Folks,

 

I been working on a trigger lately which is an AFTER INSERT on Lead. What it does is, it finds for a match in CONTACTS for the Lead's Email DOMAIN! So I've created a map for Email domains of the Leads getting inserted. However, in order to get Contact's Email Domains, I am having to fetch all the contacts. And that's where I am getting the following.

 

 

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)

 

 

So far I've come to know that we can either make the field as External ID or custom index the field or improve the query. I don't think any of these would fit in as a solution to my issue.

 

Any help of this would be much appriciated!

 

Thank you!

 

 

 

 

 

Vinita_SFDCVinita_SFDC

Hello,

 

The only way to avoid this error, which is because governor limit is hitting, is to optimize the query. Optimization can be done with the help of:

 

1. You may find that the query in question needs to be more selective in the WHERE clause. I.e. as per salesforce.com standards & best practices - the where clause needs to subset 10% or less of the data.

2. You may need a custom index on the field.

3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.

To make the field an external ID simply go to Setup | Customize | Object name | Fields, edit field name. and check the box "External ID".

 

I would suggest you to refer following links:

 

https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-make-my-SOQL-query-selective-And-the-process-to-determine-the-fields-that-can-be-custom-indexed&language=en_US

 

https://help.salesforce.com/apex/HTViewSolution?urlname=Custom-indexes-for-an-organization-to-help-improve-performance-1327109991720&language=en_US

Kevin DesaiKevin Desai

Thanks for the reply Vinita! Unfortunatly none of the options can be useful here. Email is a standard field so no externam ID. Salesforce won't create Cutom Index as my query doesn't really have a search criteria. And my Business will not come up with some better criteria so not the first one too :(