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
tonantetonante 

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

Hi, I get this error but the difference is that my SOQL queries actually return a value. I make sure they are  of size limit 1 or no greater than 4 in the ones which were flagged as errors. I have not deployed code in nearly 1.5 months , at 78% code coverage, and just last Thursday our SFDC Admin was attempting to deploy workflows using change sets and suddenly got 25 of these "Non-selective query" errors. None of the 25 errors have SQOL queries that  return Null because the queries use no variables  in the WHERE clause. Here is a sample of the SOQL code issues I am having: The "Non-Selective query" error occurs at the last query within this code segment. 
@isTest (seeAllData=true)
public class TestOpportunityAfterUpdate {
	public static testmethod void TestAdvocacyShipCharge(){
		AdvocacySupplyShipCharge_Test.TestAdvocacySupplyShipCharge();
	}
   public static testmethod void TestMozoAccounts(){
      List<Opportunity> Orders = new List<Opportunity>();
      Set<Id> contactIds = new Set<Id>();
      List<Id> orgIds = new List<Id>();
      User ouser = [SELECT Id from User where Name = 'Awana Applications' limit 1];
      RecordType rec = [Select Id from RecordType where SObjectType = 'Opportunity' and Name = 'Orders'];  
      Map<Id,Opportunity> mozoMap = new Map<Id,Opportunity>(); 
     
      List<Account> accts=[Select Id, Default_Shipping_Address__c,   Mailing_Address_Book__c,Registration_Level__c, Registration_Status__c, Date_Inactive__c,ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry 
                from Account WHERE RecordType.Name = 'US Organization' and Date_Inactive__c <> null and Account_On_Hold__c = false and RecordType.Name in ('Canada Church','US Organization')  limit 4];
    
     system.debug('<< ACCOUNTS>> '+accts);   
     
     for(Account acct : accts){
       orgIds.add(acct.Id);
     }

Here is the Production Workbench Query Result from the last query of the code above. Thanks much for your help.:
Non-Selective Query error on valid queries that return valid row sets of only 4.
Shashikant SharmaShashikant Sharma
The reason for this error is that you are querying a Table ( Object ) without a filter on Indexed field which has moe than 100000 records. There are two solutions:

1. Filter by an Index field, like CreatedDate or Id etc or any external id field
2. As this is test class you could resolv it by making seeAllData=false instead of seeAllData=true, in case if you want some results from this query than you should create Account record in test class it self instead of using Organization data.

Thanks
Shashikant
tonantetonante
OK Shashikant, thanks let me get back to you after trying option # 2 that you gave me to see if that works.  There are 13 ftest classes that are coming up with this error and it may make it much slower to recode but I will update and then do a validation deployment to see if those errors still occur and let you know. Thanks
Shashikant SharmaShashikant Sharma
Yes option #2 is what best practice as well as Test Methods should always be Org Data Independent. Please update the post as well so that others could be benifitted from it.

Thanks
Shashikant
tonantetonante
Thanks Shashikant:  I just converted my test class to not use the seeAllData and ran a validate-deployment and it was not included among the list of test classes causing the NonSelective Queries error.
Shashikant SharmaShashikant Sharma
Great, Let me know if you face any issues in deployent although it should be fine as you already validated. Please accept the post as solved by accepting solution so that others could be benifitted from if they have same issues.

Thanks
Shashikant