function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
asadim2asadim2 

Querying large tables

The following SOSL fails, even when re-written in SOQL or used in a query for-loop, when there are 100K+ Contacts in the org:
FIND :value1 IN ALL FIElDS RETURNING Contact(Id WHERE PKG__Field__c = :value2 OR (Lastname = :value3 OR Name = :value4))
The error is: System.QueryException: Non-selective query against large object type (more than 100000 rows)

I know why it's failing but I don't know how to fix it! I can't split this into 2 queries/searches either because the where-clause is ORed which means I have no choice but to query with the custom non-indexed field.

Note that this SOSL only fails when ran in a trigger!
James LoghryJames Loghry
You might want to try a batch job (or use the queable interface) to break up the SOSL query into batches of records.  Here's a SSE post I came across describing how this can be accomplished: http://salesforce.stackexchange.com/questions/53312/sosl-query-in-batch-apex

Otherwise, you're looking at limiting the result set in your trigger somehow, perhaps by applying filters on related accounts or contacts according to what you're trigger is currently iterating through.
asadim2asadim2
Batch breaks the synchronous execution path -which won't work in my case. The trigger is off of Account while the query is on Contact, and I want to query for all Contacts not just the related ones.