I am using the following query in beforeUpdate and after insert:

SELECT OwnerId ownerIdVal, Count(Id) oppCount 
FROM Opportunity 
WHERE  OwnerId IN: ownerIdSet 
AND lastmodifieddate > 2016-01-01T00:00:00.000+0000
AND StageName IN:oppStatusSet

Will Salesforce not make it non-selective since it is having a WHERE clause on OwnerId? I tried this in fullcopy sandbox and not hitting any error. Please let me know if you see any issues with this.

1 Answer 1


This query certainly could be made non-selective, if either ownerIdSet or oppStatusSet contain a null value. Another issue to worry about is ownership skew. If you have one user who owns tens of thousands of records and they get included in your filter collection, you may again run into selectivity issues.

  • Adrian, Thanks for the reply.I tried this in fullcopy sandbox having almost same number of records and did not see issues. So any idea if I will hit litmits in production?
    – Krishna
    Commented Apr 10, 2017 at 20:06
  • 1
    I already laid out the main concerns you need to watch out for. Whether your production behavior will match what you observed during functional testing will depend entirely on your QA rigor. You could try to run it by passing a record collection which will result in your top 200 record owners being in the ownerIdSet and that will give you the absolute worst case.
    – Adrian Larson
    Commented Apr 10, 2017 at 20:08
  • 1
    @Krishna OwnerId is indexed, so as long as your ownerIdSet does not contain null, you should probably be okay. Consider adding a LIMIT clause to reduce the odds.
    – sfdcfox
    Commented Apr 10, 2017 at 20:08
  • 1
    @sfdcfox Don't LIMIT clauses get pretty funky with aggregate queries?
    – Adrian Larson
    Commented Apr 10, 2017 at 20:10
  • 1
    @Krishna please do not ask new questions via comments. Note threat negative filters such as != increase the cost.
    – Adrian Larson
    Commented Apr 12, 2017 at 3:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .