Contents
When to terminate a non-selective soql query?
To avoid long execution times, the system can terminate nonselective SOQL queries. Developers receive an error message when a non-selective query in a trigger executes against an object that contains more than 200,000 records. To avoid this error, ensure that the query is selective.
When is a soql query selective in apex?
A query is selective when one of the query filters is on an indexed field and the query filter reduces the resulting number of rows below a system-defined threshold. The performance of the SOQL query improves when two or more filters used in the WHERE clause meet the mentioned conditions.
How to work with very large soql queries?
Working with Very Large SOQL Queries Your SOQL query sometimes returns so many sObjects that the limit on heap size is exceeded and an error occurs. To resolve, use a SOQL query for loop instead, since it can process multiple batches of records by using internal calls to query and queryMore.
When does non-selective query occur in Salesforce?
Sample error: System.QueryException: Non-selective query against large object type (more than 100000 rows). This usually happens in the context of a trigger because Salesforce is careful to make sure that transactions and therefore locks do not last very long. Note very specifically that it occurs in a trigger.
What is a non selective query in Salesforce?
First exception on row 0 with id a0Tb000000E6kACEAZ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClientInvoiceAfterUpdate: execution of AfterUpdate caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
How to check if your soql is using an index?
You can use the Developer Console to check if you SOQL is going to use an Index: Setup->Developer Console. Enable the Query Plan tool in Preferences (Help -> Preferences). Then you can execute your SOQL Query Plan in the Query Editor Pane. 2.
When does the performance of a soql query improve?
The performance of the SOQL query improves when two or more filters used in the WHERE clause meet the mentioned conditions. The selectivity threshold is 10% of the first million records and less than 5% of the records after the first million records, up to a maximum of 333,333 records.