Apex code may receive a "System.QueryException: Non-selective query against large object type" error when running a SOQL query in an Apex trigger context, even if the same query succeeds when run in the Developer Console.
Full error:
"System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing."
A query may still be non-selective even when a field is indexed if:
By design, non-selective queries on large object types are not allowed within an Apex trigger context. This restriction prevents non-deterministic query execution time — which can result from full or partial table scans — from degrading the end user's experience when saving a record.
NOTE: If this restriction were not in place and a non-selective query ran in a trigger invoked on record save, the end user could be blocked waiting for the query to time out before receiving any UI feedback.
The restriction does not apply in the Developer Console or via SOAP/REST API, where a standard query timeout applies instead.
Optimize the SOQL query to leverage an index. Refer to the Salesforce Query Search Optimization Developer Cheatsheet for guidance on writing selective, index-friendly queries. 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.