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
AbAb 

Errror for System.QueryException:

Hello,

I got error message like below:
caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

 
Best Answer chosen by Ab
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

According to Salesforce knowledge article (https://help.salesforce.com/articleView?id=000002493&type=1):

If you're running a trigger on objects that have more than 200,000 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type." We'll go over a few possible fixes.

Resolution: Options to resolve error:

1. You may find that the query in question needs to be more selective in the WHERE clause. According to the Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.

2. A custom index (https://help.salesforce.com/articleView?id=000232453&type=1&language=en_US) on the field.

3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.

In Classic, go to Setup -> Create -> Objects then click the Object name. 
In LEX, go to Setup -> Object Manager then Object name.
Then, edit field name and check the box External ID.

NOTE: If this does help your query performance, we recommend that you still log a case with support to implement a custom index in its place. This is not a long term solution for performance improvements and has other designed use cases.

Read our Make SOQL query selective (https://help.salesforce.com/articleView?id=000006044&type=1) help article for more information on indexing.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

According to Salesforce knowledge article (https://help.salesforce.com/articleView?id=000002493&type=1):

If you're running a trigger on objects that have more than 200,000 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type." We'll go over a few possible fixes.

Resolution: Options to resolve error:

1. You may find that the query in question needs to be more selective in the WHERE clause. According to the Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.

2. A custom index (https://help.salesforce.com/articleView?id=000232453&type=1&language=en_US) on the field.

3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.

In Classic, go to Setup -> Create -> Objects then click the Object name. 
In LEX, go to Setup -> Object Manager then Object name.
Then, edit field name and check the box External ID.

NOTE: If this does help your query performance, we recommend that you still log a case with support to implement a custom index in its place. This is not a long term solution for performance improvements and has other designed use cases.

Read our Make SOQL query selective (https://help.salesforce.com/articleView?id=000006044&type=1) help article for more information on indexing.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Sandrine,

Please Try these steps :
    
If you're running a trigger on objects that have more than 200,000 records, it's possible that you'll receive the error, "System.QueryException.

You may find some steps in the below link to resolve this error:
https://help.salesforce.com/articleView?id=000002493&type=1

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha