+ Start a Discussion
World IndiaWorld India 

what is database.getquerylocator & how many minimum and maximum records fetched from database

SandhyaSandhya (Salesforce Developers) 
Hi,

database.getQueryLocator returns a Query Locator that runs your selected SOQL query returning list that can be iterated over in bathc apex or used for displaying large sets in VF (allowing things such as pagination). 


According to the documentation, in a batch start() method the governor limit is bypassed. The governor limit of 10,000 in the limits cheatsheet still applies in execute() and finish() and other methods.
Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. If you use a querylocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an organization. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an organization.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_batch.htm

http://salesforce.stackexchange.com/questions/64805/governor-limits-database-getquerylocator

Hope this information 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

 
Ashu sharma 38Ashu sharma 38
Hello Sandhya...
Nice explanation,but i have still doubts in this,that as the synchronous operation have :10000 records retrived via database.getQueryLocator ,so how we can fetch 50 million records??

And one more doubt that as Batch apex is async.Operation and in cheatsheet given that 10000 records we can fetch via databse.getQuerylocator...in synchoronus Limit????

Thanks...
sanjay_baroliyasanjay_baroliya
a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org
Ayush Goyal_RangerAyush Goyal_Ranger

Hello Ashu Sharma,
Database.getQueryLocator will return 50 million records only if it is used in Start method of the class which implements the Database.Batchable interface. If you use QueryLocator methods in execute or finish methods, the governor limits are not bypassed and will only return 10,000 records.

The same goes with synchronous apex. If you use this method in synchronous apex methods, the QueryLocator method is going to return only 10,000 records, which is even lesser than simple SOQL query which returns 50,000 rows per transaction. So the best practice to get records and bypass the governor limits is to use the QueryLocator in start method of batch apex.