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
hiteshwar marnihiteshwar marni 

Database query

I am not able to get a clarity on the Database.query() , Database.getQueryLocator and Database.QueryLocator.I have gone through many documents but no use.Could any one help me on this.
Thanks
SandhyaSandhya (Salesforce Developers) 
Hi,

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

Please refer this link.

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

Hope this helps you!

If this helps mark it as solved.

Thanks and Regards
Sandhya
Naveen DhanarajNaveen Dhanaraj
Database.query():
We can retrieve up to 50,000 records.
If VF page doesn’t have read only attribute, use Database.query().
In Batch Apex, if we use Database.query(), it supports 50,000 records only.

Database.getQueryLocator:
We can retrieve up to 10,000 records.
If VF page have read only attribute, use Database.getQueryLocator().
In Batch Apex, if we use Database.getQueryLocator(), it supports upto 50 million records.
hiteshwar marnihiteshwar marni
Hi Naveen Dhanaraj,

could you please let me know why to use Database.query() If VF page doesn’t have read only attribute and  Database.getQueryLocator() If VF page have read only attribute.
Thanks