Loading

SOQL Error System.QueryException: Non-selective query against large object type

Publiseringsdato: Jun 22, 2026
Beskrivelse

When running a SOQL (Salesforce Object Query Language) query against a Salesforce object that contains more than 200,000 records, you may receive the following error, especially when the query runs inside an Apex trigger:
"System.QueryException: Non-selective query against large object type"

When This Error Occurs

This error is thrown when the Salesforce SOQL query optimizer evaluates the filters in your query and cannot identify a selective filter — a filter that uses an indexed field and is estimated to match fewer than 10% of the total records in the object. Without a selective filter, querying a large object would require a full table scan, which is not permitted inside triggers.

Løsning

How Salesforce Determines Query Selectivity

The Salesforce query optimizer analyzes the WHERE clause of your SOQL query. A filter is treated as selective when:

  1. It is based on an indexed field (standard indexed fields include Id, Name, OwnerId, CreatedDate, and fields with the External ID flag enabled)
  2. The optimizer heuristically determines the filter value matches fewer than 10% of the total records in the object

If no selective filter is found, the query is marked as non-selective and the QueryException is thrown.

Quick Fix: Enable External ID on the Filter Field

If your WHERE clause filters on a custom text field (for example, Text_Field__c), enabling the External ID flag on that field creates an index, which may make the query selective.
Steps to enable External ID:
In Lightning Experience:

  1. Go to Setup > Object Manager
  2. Select the object (e.g., Account)
  3. Click Fields & Relationships
  4. Click Edit next to the field name
  5. Check the External ID checkbox
  6. Save

In Salesforce Classic:

  1. Go to Setup > Create > Objects > [Object Name] > Fields
  2. Click Edit next to the field name
  3. Check the External ID checkbox
  4. Save
Important: This fix only resolves the error if the filter value you are using in the WHERE clause matches fewer than 10% of the records. If the filter value is too common (for example, filtering on a Status field where most records have the same value), the query will remain non-selective even with an index.

If the Issue Persists

If enabling External ID does not resolve the error, log a case with Salesforce Support. The Support team will:

  • Evaluate whether any other fields are candidates for custom indexing
  • Recommend query modifications (such as adding additional filters to reduce the result set)
Knowledge-artikkelnummer

000384950

 
Laster
Salesforce Help | Article