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
Ketan Thakare 2Ketan Thakare 2 

how to retrieve single record quickly from database having 20 million records

Lokesh KumarLokesh Kumar
Hi Ketan,

You can Use SOQL like below approach.
 
List<Account> lstAccount = [SELECT Name From Account where name = 'Unknown Account'];

Thanks,
Lokesh
Amit Chaudhary 8Amit Chaudhary 8
Make SOQL query selective
1) https://help.salesforce.com/articleView?id=How-to-make-my-SOQL-query-selective&language=en_US&type=1

Working with Very Large SOQL Queries
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm

More Efficient SOQL Queries
For best performance, SOQL queries must be selective, particularly for queries inside triggers. To avoid long execution times, the system can terminate nonselective SOQL queries. Developers receive an error message when a non-selective query in a trigger executes against an object that contains more than 200,000 records. To avoid this error, ensure that the query is selective.
Selective SOQL Query Criteria
  • A query is selective when one of the query filters is on an indexed field and the query filter reduces the resulting number of rows below a system-defined threshold. The performance of the SOQL query improves when two or more filters used in the WHERE clause meet the mentioned conditions.
  • The selectivity threshold is 10% of the first million records and less than 5% of the records after the first million records, up to a maximum of 333,333 records. In some circumstances, for example with a query filter that is an indexed standard field, the threshold can be higher. Also, the selectivity threshold is subject to change.
User-added image

Examples of Selective SOQL Queries
To better understand whether a query on a large object is selective or not, let's analyze some queries. For these queries, assume that there are more than 200,000 records for the Account sObject. These records include soft-deleted records, that is, deleted records that are still in the Recycle Bin.
Query 1:
SELECT Id FROM Account WHERE Id IN (<list of account IDs>)
Query 2:
SELECT Id FROM Account WHERE Name != ''
Since Account is a large object even though Name is indexed (primary key), this filter returns most of the records, making the query non-selective.

Query 3:
SELECT Id FROM Account WHERE Name != '' AND CustomField__c = 'ValueA'
Here we have to see if each filter, when considered individually, is selective. As we saw in the previous example, the first filter isn't selective. So let's focus on the second one. If the count of records returned by SELECT COUNT() FROM Account WHERE CustomField__c = 'ValueA' is lower than the selectivity threshold, and CustomField__c is indexed, the query is selective.

Improve performance with Custom indexes using Selective SOQL Queries
1) https://help.salesforce.com/articleView?id=000006007&type=1

Improve SOQL Query Performance - Selectivity and Custom Indexes
1) https://help.salesforce.com/articleView?id=Checklist-for-Custom-Index-Requests&language=en_US&type=1


Let us know if this will help you