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

"Non-selective query against large object type" error


I have a table containing over 270,000 objects.  When I try to insert more objects, I get an error saying "Non-selective query against large object type" from a before-insert trigger that I have on that table.

The before-insert trigger is to prevent duplicate rows being inserted based on a formula field:

trigger XBeforeInsert on X__c (before insert) {

	Map<String, X__c> xs = new Map<String, X__c>();
	for (X__c x: {
			xs.put (x.Unique__c, x);

	for (X__c x: [ Select Id, Unique__c, RecordTypeId
				 From X__c
				  Where Unique__c in :xs.keySet()]) {
		// add some error message
The field Unique__c is not indexed.  Would getting Salesforce to add a custom index on that field make a difference?  Alternatively, is there some other way to achieve this?



You have 2 ways to remove this error :-

1.) You can add more filters in the query.
2.) You can contact support to get your field Unique__c custom indexed provided it matches the indexing criteria.That would certainly help.

If this helps,please mark it as best answer to ehlp others :)
Scott McClungScott McClung
You can also set the Unique__c field as an external ID which will cause the system to index it.

Here is a query optimization cheet sheet.  In the Related Resources section you'll find some articles that expand on the subject of selective queries.
Unfortunately Unique__c is a formula field, so I don't believe that it can be set as an external id