+ Start a Discussion
Ron WildRon Wild 

Non-selective Query Error in Apex Trigger

Hi,

I'm getting the following non-selective query error in one of my Apex triggers:

Error running cleanRecords method.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)

Here's the query statement ... isn't this selective?  I have WHERE and LIMIT clauses and put in hard-coded values for status and subject to make sure I wasn't passing in Null values.


Code:
for ( List<Task> tasks : [Select Id from Task where Status like 'Completed' 
and Subject like 'Delete Me'
and CreatedDate < :(System.now().addDays(0-days+1)) Limit 100]) { numDeleted += tasks.size(); delete tasks; }

Any help would be much appreciated.
 
Thanks,

Ron
SuperfellSuperfell
what happens if you use = instead of like, i'm not sure if like would ever be considered selective.
Ron WildRon Wild
I tried "=" instead of  "like"  ... and got the same error.


Ron WildRon Wild
I've also tried filtering null records (as suggested in another thread):

Code:
for ( List<Task> tasks : [Select Id from Task where Status like 'Completed' 
                          and Subject != null
                          and Status != null
                          and Subject like 'Delete Me%' 
                          and CreatedDate < :(System.now().addDays(0-days+1)) Limit 100]) {
    numDeleted += tasks.size();
    delete tasks;
}

Crikey, what's it going to take to convince the query optimizer to let this one go through?



AcMEGXAcMEGX

Hi Ron,

 

I know this post is old however did you find a solution to your problem. Can you try adding a workflow (that will fire everytime the record is created/updated) to copy 1 of your criteria (email/phone/etc) to a custom indexed field? Use that indexed field as part of your "where" clause to avoid the Non-selective query error