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
jagadeesh chandu 3jagadeesh chandu 3 

How to fetch more than 50 million records using database querylocator in batch apex?

 what happens if it fails?
Best Answer chosen by jagadeesh chandu 3
Anup JadhavAnup Jadhav
You can't fetch more than 50 million records because there is a platform governor limit. If you have more than 50 million I think it'll throw a limit exception. The workaround around would be to specify a filter condition that allows you to query it in the multiple batches. So it'd would look something like this.

String query1 = 'select id from account where recordCount__c <= :5000000';
String query2 = 'select id from account where recordCount__c > :500000'  and <= :1000000';

then use query1 and query2 with queryLocator. Note that recordCount__c is a custom field that you can auto-populate using a trigger or workflow.

Regards,
Anup