Apex code may receive a "System.QueryException: Non-selective query against large object type" error when attempting to run a SOQL query via an Apex trigger context. The full error is as follows:
"System.QueryException: Non-selective query against large object type (more than 200000 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:
However, the same query runs successfully when run via the Developer Console (i.e. anonymous Apex). Why does it not error out in this case too?
By design, non-selective queries on large object types are not allowed on trigger context (when evaluating Apex triggers). This design restriction prevents the impact a non-deterministic query execution time (due to a full/partial table scan required to perform the SOQL query) could have on the end-user's experience. As a result, attempts to run a non-selective query on a large object type within a trigger context will be prevented, and the error message will be displayed.
NOTE: If this restriction wasn't in place, and the affected query was run within a given trigger that gets invoked upon saving a record, in a worst case scenario, the end user would need to wait for the query to time out to get some UI feedback.
The same restriction does not apply when the query is run within the Developer Console or via the SOAP/REST API. In this case, a standard query timeout will apply. In other words, the query will be attempted, and will be timed out only if it takes longer than a certain time.
It is suggested that the affected SOQL query be optimized so that it can leverage an index. See Salesforce query search optimization developer cheatsheet for more details on how to optimize a SOQL query.
000386468

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.