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
Diwakar G 7Diwakar G 7 

Problem with Attachment object

Hi,
I have written the following soql query for fetching the last created record.

SELECT id, name,Body,parentID FROM Attachment order by CreatedDate DESC limit 1

But I am getting this error. Please help me. Thanks in advance.

System.QueryException : Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
AshishkAshishk
Create external id field with default value and add condition as where externalid=value, it should work.

Or log a case with salesfore.
Diwakar G 7Diwakar G 7
Can you please elaborate it.
Raj VakatiRaj Vakati
Try like this
 
SELECT id, name,Body,parentID FROM Attachment where Name ='Test'order by CreatedDate DESC limit 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 the a few possible fixes. 


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 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. ( OR YOU CAN USE STANDARD INDEX fields also ) 

NOTE: If this does help your query performance, we recommend that you still log a case with support to implement a custom index in it's place. This is not a long term solution for performance improvements and has other designed use cases.
Read our Make SOQL query selective help article for more information on indexing.

 
NimitNimit
Try to add where criteria in the SOQL which will limit the no. of records in result