You need to sign in to do that
Don't have an account?
ChickenOrBeef
Error: Non-selective query against large object type
Hello everyone,
When our users edit a task, they receive this error:
MainTriggerTask: execution of AfterUpdate
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)
Class.ClassTaskUpdateRelatedActivity.updateRelatedActivity: line 30, column 1
Trigger.MainTriggerTask: line 30, column 1
Here is the trigger that's causing the issue:
Some background on what's going on here:
We have a custom object called "Related Activity", which is basically a copy of a task. It's attached to all the accounts related to that respective task's account. So for example, if you create a task on an account that has a parent account and two sibling accounts, then a Related Activity will be created for the parent and two siblings. It's a copy of that task.
The trigger posted above simply updates the related activites when a task has been updated. But as you can see, when our users try editing a task, they receive the error above.
Any idea why that error would occur? One thing I should mention is that there may not be any Related Activities for a given task, since the account may not have any related accounts. I'm not sure if that would cause an issue.
Let me know if you need any other info from me!
Thanks!
-Greg
When our users edit a task, they receive this error:
MainTriggerTask: execution of AfterUpdate
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)
Class.ClassTaskUpdateRelatedActivity.updateRelatedActivity: line 30, column 1
Trigger.MainTriggerTask: line 30, column 1
Here is the trigger that's causing the issue:
public class ClassTaskUpdateRelatedActivity{ public void updateRelatedActivity(List<Task> tasks, Map<ID,Task> oldTasks){ Set<String> tasksInTrigger = new Set<String>(); Map<String,String> ownerMap = new Map<String,String>(); Map<String,String> descriptionMap = new Map<String,String>(); Map<String,String> subjectMap = new Map<String,String>(); Map<String,Date> dateMap = new Map<String,Date>(); List<Related_Activity__c> relatedActivitiesToUpdate = new List<Related_Activity__c>(); FOR(Task t : tasks){ IF(t.OwnerId != oldTasks.get(t.Id).OwnerId || t.Description != oldTasks.get(t.Id).Description || t.Subject != oldTasks.get(t.Id).Subject || t.ActivityDate != oldTasks.get(t.Id).ActivityDate){ tasksInTrigger.add(t.Id); ownerMap.put(t.Id,t.OwnerId); descriptionMap.put(t.Id,t.Description); subjectMap.put(t.Id,t.Subject); dateMap.put(t.Id,t.ActivityDate); } } IF(tasksInTrigger.size() > 0){ FOR(Related_Activity__c ra : [SELECT Id,Assigned_To__c,Activity_ID__c,Description__c,Name,Date__c FROM Related_Activity__c WHERE Activity_ID__c In :tasksInTrigger]){ IF(subjectMap.get(ra.Activity_ID__c).length() > 80){ ra.Name = subjectMap.get(ra.Activity_ID__c).SubString(0,80); } ELSE{ ra.Name = subjectMap.get(ra.Activity_ID__c); } ra.Assigned_To__c = ownerMap.get(ra.Activity_ID__c); ra.Description__c = descriptionMap.get(ra.Activity_ID__c); ra.Date__c = dateMap.get(ra.Activity_ID__c); relatedActivitiesToUpdate.add(ra); } } IF(relatedActivitiesToUpdate.size() > 0){ UPDATE relatedActivitiesToUpdate; } } }
Some background on what's going on here:
We have a custom object called "Related Activity", which is basically a copy of a task. It's attached to all the accounts related to that respective task's account. So for example, if you create a task on an account that has a parent account and two sibling accounts, then a Related Activity will be created for the parent and two siblings. It's a copy of that task.
The trigger posted above simply updates the related activites when a task has been updated. But as you can see, when our users try editing a task, they receive the error above.
Any idea why that error would occur? One thing I should mention is that there may not be any Related Activities for a given task, since the account may not have any related accounts. I'm not sure if that would cause an issue.
Let me know if you need any other info from me!
Thanks!
-Greg
Just to update on this, I had to get Salesforce support to make Activity_ID__c an indexed field. That did the trick.
Thanks for your help!
-Greg
All Answers
1) So should the debug statement go in line 24, between the two FOR loops? Should I loop through every ID in "tasksInTrigger", or should I just print out tasksInTrigger[0]? When our users were editing the task, it was only one task they were editing. So I'm guessing I don't need to loop through it?
2) When our users experienced the error, there were no Related Activities, since the accounts didn't have any related accounts. So when the trigger got to the SOQL query, there were no Related Activities to find. Is that the issue?
Thanks!
-Greg
Then, in the Sandbox, I updated a task that had no Related Activities and these were the debug statements:
Then, still in the Sandbox, I updated a task that had three Related Activities and these were the debug statements:
Both edits worked in the Sandbox, but that's probably because there are only a handful of Related Activities in the Sandbox, while there's over 100,000 Related Activities in Production.
Any thoughts on next steps? Thanks so much for your help so far!
And if that is the only possible reason, then I'm assuming your recommendation would solve it?
Does that change things? Or could a NULL value in 'tasksInTrigger' still be the issue?
and see if you have any that's got weird data. If nothing stands out, I'd say push the debug code to your prod sandbox and monitor a user while they make it fail. See if you get any better data out of it then.
Sorry for the delayed response ( a few other things came up), but where should I make that query?
Anywho, I tried making that query in Workbench, but I received the following error. Let me know what I'm doing wrong:
Here are the results. It looks like none of them have a blank Activity ID.
I then did a simple query for any Related Activity that had a NULL Activity ID, and there were none:
Thoughts on next steps?
that should count the number of related_activity__c objects grouped by activity_id__c
There's one other class that's giving me the "Non-selective query" error as well:
The Related Activity object has an Account look-up field called "Related To", so if an Account gets deleted, that "Related To" field will turn blank if the deleted Account was in that field. So this trigger simply runs after an Account gets deleted, it queries all Related Activities with a NULL "Related To" value, and it deletes them.
When I tried merging two Accounts, I received the "Non-selective query" error. Is this easier to figure out? Could it be related?
Thanks!
-Greg
After doing a bit more research on this, it turns out that a query can be "non-selective" if it's querying an object that has over 100,000 total records, even if the query itself isn't returning 100,000 records.
There are indeed over 100,000 Related Activities, so from what I understand I have to simply include more filters or more indexed fields. The problem is that I'm not sure which filters/indexes or how many filters/indexes I need to make the query selective.
Do you have any thoughts on what filters or indexes I should use?
Thanks!
-Greg
I'm suprised that it is complaining about that for your query. I would think that your Activity_Id__c (being a lookup relationship) would be a selective query. It may only consider it selective if it's a M/D relationship instead of a lookup.
The only way you can make this work (assuming Related_To__c is also a lookup) is to change it to a Master/Detail.
PS: Sorry for the delay, it's been a crazy week.
I think the confusion here is that Activity_ID__c is NOT a lookup field. It's just a text field. So it's not indexed. Do I just need to ask Salesforce support to make "Activity_ID__c" indexed? Or perhaps I should just change it to a lookup field?
And no worries about the response time! Thanks for all your help so far.
As for having SFDC index it, I'm not sure if they can do that.
Just to update on this, I had to get Salesforce support to make Activity_ID__c an indexed field. That did the trick.
Thanks for your help!
-Greg