1

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
GROUP BY OwnerId

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.

3

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.

| improve this answer | |
  • 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 Apr 10 '17 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 Apr 10 '17 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 Apr 10 '17 at 20:08
  • 1
    @sfdcfox Don't LIMIT clauses get pretty funky with aggregate queries? – Adrian Larson Apr 10 '17 at 20:10
  • 1
    @Krishna please do not ask new questions via comments. Note threat negative filters such as != increase the cost. – Adrian Larson Apr 12 '17 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.