+ Start a Discussion
sfdcjoeysfdcjoey 

how to optimise soql query

There is a simple SOQL query ‘Select Id, Name from Opportunity’ which fetches millions of records without any selection criterion. How do we optimize the performance of this simple query without using any selection criterion ?
SandhyaSandhya (Salesforce Developers) 
Hi,

The performance of a SOQL will depend on the presence of a selective filter. If a SOQL query contains at least 1 selective filter, the query is said to be selective. If the SOQL query doesn't contain a selective filter, the query is said to be un-selective and will require a full table scan.Consider an indexed filter 

Please refer below link which explains about this.

https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US
 
http://www.salesforcegeneral.com/salesforce-articles/writing-more-efficient-soql-queries.html
 
https://support.workato.com/support/solutions/articles/1000236530-improving-soql-query-performance-with-indexed-fields
 
https://help.salesforce.com/HTViewSolution?id=000181277
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya