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
Prema -Prema - 

How many records returned by Database.Query, please let me know and where should we use database.getQueryLocator and Database.Query.

Best Answer chosen by Prema -
Raj VakatiRaj Vakati
1. Database.query() retrives 50K records where as Database.getQueryLocator() retrives 50 million records from Database.
2. Database.query() is used to construct dynamic query instead of using SOQL query where as Database.getQueryLocator() is the return type of start method of a Batch class.

database.query allows you to make a dynamic SOQL query at runtime. You can build up a string and then use that as a query string at run time in the database.query statement to make a SOQL call that is determined at run time.

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).

The query locator can return upto 50 million records and should be used in instances where you want to bactha a high volume of data up (pagination or batch apex). The database.query method should be used in instances where you are wanting to do a dynamic runtime SOQL query for your code.

Triggers are run in a particular context whenever a record is inserted, updated, deleted or undeleted and either are run before or after depending upon their type. Typically they are used for complex validation and business logic or data preparation before an object is modified on the database.

Batch apex is an asynchronous processing of a large volume of record 200 at a time. You would use bacth apex when you wanted to run a process that updated a large volume of records and you do not need the result to be instant.

 

All Answers

Raj VakatiRaj Vakati
The query locator can return upto 50 million records and should be used in instances where you want to bactha a high volume of data up (pagination or batch apex). The database.query method should be used in instances where you are wanting to do a dynamic runtime SOQL query for your code.



Database.Query() - It will allows you build a dynamic query at runtime. Example - You build a dynamic query and keep it in string variable and use that string variable in the database.query will return upto 50000 K
Raj VakatiRaj Vakati
1. Database.query() retrives 50K records where as Database.getQueryLocator() retrives 50 million records from Database.
2. Database.query() is used to construct dynamic query instead of using SOQL query where as Database.getQueryLocator() is the return type of start method of a Batch class.

database.query allows you to make a dynamic SOQL query at runtime. You can build up a string and then use that as a query string at run time in the database.query statement to make a SOQL call that is determined at run time.

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).

The query locator can return upto 50 million records and should be used in instances where you want to bactha a high volume of data up (pagination or batch apex). The database.query method should be used in instances where you are wanting to do a dynamic runtime SOQL query for your code.

Triggers are run in a particular context whenever a record is inserted, updated, deleted or undeleted and either are run before or after depending upon their type. Typically they are used for complex validation and business logic or data preparation before an object is modified on the database.

Batch apex is an asynchronous processing of a large volume of record 200 at a time. You would use bacth apex when you wanted to run a process that updated a large volume of records and you do not need the result to be instant.

 
This was selected as the best answer
Prema -Prema -
If possible could you please explain me with an example like we face in real worrld so that we need to go for database.queery instead of database.getquerylocator orr both. I would really appreciate
Raj VakatiRaj Vakati

http://www.infallibletechie.com/2013/05/difference-between-databasequery-and.html
https://thysmichels.com/2013/01/15/static-and-dynamic-soql-queries-and-best-practices/

Database.query():

We can retrieve up to 50,000 records.
 In Batch Apex, if we use Database.query(), it supports 50,000 records only.
If you wanted to prepare dynamic query from the string as show below we will use Database.query()

 
String fieldName = ‘Name,Phone’;
String dynQuery = ‘select Id ‘ + fieldName + ‘ From Account’;
Database.query(dynQuery);


Database.getQueryLocator

Database.getQueryLocator() is the return type of start method of a Batch  
 
private String query = 'Select id from Account';
return Database.getQueryLocator(query);