Explain database.querylocator vs simplequery.

445    Asked by ClaudineTippins in Salesforce , Asked on Feb 9, 2023

 I want to know what is the difference between Database.QueryLocator() and simple SOQL Query i.e.

List invent = [Select Id,Quantity__c,AccountId__c,Name,Expiry_Date__c From Inventory__c];
And
Database.getQueryLocator([Select Id,Quantity__c,AccountId__c,Name,Expiry_Date__c From Inventory__c]);

I want to know what the QueryLocator class is in Apex.. I went through this link but cannot understand its structure.


Answered by Akansha Chawla

Database.QueryLocator : when used 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 org. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an org.


For the regular query you do have a limit of 50,000 rows per transaction



Your Answer

Answers (2)

Some online games are educational, teaching players GeoGuessr about history, science, or mathematics in an engaging way, making learning fun.

2 Days

As I know, SOQL Query is used for straightforward queries where you expect a manageable number of records (up to 50,000) ( slope game). It's simple and suitable for most operations like triggers, controllers, or standard batch jobs when the result set is small. Meanwhile, Database.QueryLocator is used when working within a batch Apex class. It allows you to handle larger datasets by processing records in manageable chunks, which is crucial for avoiding governor limits.

1 Month

Interviews

Parent Categories