2

Is there any advantage of using operator "IN" instead of "NOT IN" in SOQL? Are there any performance benefits of it?

I have a use case where I have to filter the records based on a certain column, say Region, which has many distinct values. NOT IN is preferable since only a fraction of those distinct values will be used for filteration.

e.g. let there be 10 distinct values in Region ... 1. NJ 2. NY 3. .. ..... 10. CL

the NOT IN will only contain, say NJ and NY

1
6

I believe using selective query like IN is preferable over NOT IN.

When your filter uses != or NOT—which includes using NOT EQUALS/CONTAINS for reports, even if the field is indexed—the Force.com query optimizer can’t use the index to drive the query. For better performance, filter using = or IN, and the reciprocal values.

https://developer.salesforce.com/blogs/engineering/2013/07/maximizing-the-performance-of-force-com-soql-reports-and-list-views.html

0

When you have IN in your where clause, the query restricts the records then writing NOT IN.

In the above scenario, it will not make much any unless you are trying to fetch all the records based on Region not having NJ and NY

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.