+ Start a Discussion
ram sf 10ram sf 10 

Approaching limit for non-selective query against large object type (more than 100000 rows).

Hi Everyone,

I am new to Salesforce Apex Coding and would greatly appreaciate if someone can help me out.

We have custom object called "Custom Payment" with a lookup field to Contact.
The following is my Apex Class that I am using as an Extension for a Visualforce Page to autopopulate the information from Contact Object when the Users Select a Contact on the Visualforce Page.

Everything is working fine and the record is also getting saved but than I recieve the Apex Governor Limit Warning emails from Salesforce.

Approaching limit for non-selective query against large object type (more than 100000 rows). Current size is approximately 90261 rows. When the limit is reached, the query will fail. 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) null

public class manualpaymentcntrl{
    public Custom_Payment__c ptxn { get; set; }
    public Contact con { get; set; }
public manualpaymentcntrl(ApexPages.StandardController std) {
    ptxn=new Custom_Payment__c ();//Create a new instance 
    con=new Contact();

public pagereference obtaindetails() {
       for ( Contact con : [select FirstName,LastName,Email,AccountID,Phone,MobilePhone,Title, MailingStreet,MailingCity,MailingState,MailingCountry,MailingPostalCode from Contact where Id = :ptxn.Contact__c]){
        ptxn.FirstName__c = con.FirstName;
        ptxn.LastName__c = con.LastName;
        ptxn.Email__c = con.Email;
        ptxn.Account__c = con.AccountID;
        ptxn.Phone__c = con.Phone;
        ptxn.MobilePhone__c = con.MobilePhone;
        ptxn.Salutation__c = con.Title;
        ptxn.MailingStreet__c = con.MailingStreet;
        ptxn.MailingCity__c = con.MailingCity;
        ptxn.MailingState__c = con.MailingState;
        ptxn.MailingCountry__c = con.MailingCountry;
        ptxn.MailingPostalCode__c = con.MailingPostalCode;
        ptxn.Payment_Setting__c = 'a0x900000012re5';
        ptxn.Frequency_Type__c = 'a0p9000000opqn1';
    return null;

    public PageReference save(){
        upsert ptxn;
        PageReference reRend = new PageReference('/apex/custommanualpayment');
        return reRend;
    public PageReference cancel(){
        PageReference reRend = new PageReference('/apex/home/home.jsp');
        return reRend;

Thanks In Advance......
PratikPratik (Salesforce Developers) 
Hi Ram,

For best performance, SOQL queries must be selective, particularly for queries inside of triggers. To avoid long execution times, non-selective SOQL queries may be terminated by the system. Developers will receive an error message when a non-selective query in a trigger executes against an object that contains more than 100,000 records. To avoid this error, ensure that the query is selective.

To Make query selective:
Selective SOQL Query Criteria

1.A query is selective when one of the query filters is on an indexed field and the query filter reduces the resulting number of rows below a system-defined threshold. The performance of the SOQL query improves when two or more filters used in the WHERE clause meet the mentioned conditions.

2. The selectivity threshold is 10% of the records for the first million records and less than 5% of the records after the first million records, up to a maximum of 333,333 records. In some circumstances, for example with a query filter that is an indexed standard field, the threshold may be higher. Also, the selectivity threshold is subject to change.

You can also think of having custom indexing on the fields in the query.

Please go through this article:


P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
Bhawani SharmaBhawani Sharma
Bryan Leaman 6Bryan Leaman 6
Make sure you don't execute the query in "obtaindetails" when ptxn.Contact__c is null.