home Homebuild Toolsbug_report Errorsmenu_book Guideslightbulb Tipssmart_toy Promptsextension Extensionsfolder_open Resourcesinfo About
search
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.

What does this error mean?

This is the same underlying error as a non-selective query, but thrown specifically in the context of Apex triggers or classes running against large objects — particularly important in trigger contexts where the error can halt record saves entirely. Salesforce requires that queries on objects with 200,000+ records use at least one selective filter on an indexed field.

Selectivity is calculated per-filter: a filter is selective if it reduces the result set below 10% of the first million records, or below 5% of records beyond one million, with a maximum of 333,333 records. If no individual filter meets the threshold, the query is considered non-selective.

Common Causes

1. Trigger queries not scoped to trigger records

A trigger that queries related records using a non-indexed field instead of the trigger's record IDs — for example, querying all cases for an account using a custom field rather than the AccountId lookup.

2. Negation operators on any field

Filters using !=, NOT IN, or NOT LIKE are inherently non-selective regardless of the field — they potentially match most records.

3. OR conditions between non-indexed fields

OR conditions prevent index use even when one of the fields is indexed, because both branches must be evaluated.

How to Fix It

Solution 1: Anchor trigger queries to trigger.newMap.keySet()

Apex
// ❌ BAD — non-indexed filter on large Case object
List<Case> cases = [
    SELECT Id FROM Case
    WHERE Custom_Category__c = 'Support'
];

// ✅ GOOD — indexed Id field, always selective
List<Case> cases = [
    SELECT Id FROM Case
    WHERE Id IN :Trigger.newMap.keySet()
];

// ✅ GOOD — indexed date range + additional filter
List<Case> cases = [
    SELECT Id, Custom_Category__c FROM Case
    WHERE CreatedDate = LAST_N_DAYS:7
      AND Custom_Category__c = 'Support'
];

Solution 2: Use the Developer Console Query Plan tool

Open the Developer Console → Query Editor → paste your SOQL → click Plan (not Execute). The Query Plan shows cardinality estimates and whether any index will be used. A "TableScan" in the plan means the query will be non-selective on large objects.

lightbulb

Pro Tip: If your use case genuinely requires filtering on a non-indexed custom field at large scale, open a Salesforce Support case requesting a custom index. Salesforce evaluates selectivity and adds the index if the field typically produces selective queries. This is free and has no ongoing cost.