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
samrat.1985@lntinfotechsamrat.1985@lntinfotech 

Total number of SOQL queries issued for Batch Apex and future methods

Hi,

How to calculate the total number of SOQL queries issued for Batch Apex and Future methods of a VF page using the Limits class or some other alternatives,

Can anyone suggest some idea?

souvik9086souvik9086

See the following

 

 

LIMITS

System.debug('Total Number of SOQL Queries allowed in this apex code context: ' + Limits.getLimitQueries());
System.debug('Total Number of records that can be queried in this apex code context: ' + Limits.getLimitDmlRows());
System.debug('Total Number of DML statements allowed in this apex code context: ' + Limits.getLimitDmlStatements() );
System.debug('Total Number of script statements allowed in this apex code context: ' + Limits.getLimitScriptStatements());

USAGE
System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
System.debug('2.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());
System.debug('3. Number of script statements used so far : ' + Limits.getDmlStatements());
System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
System.debug('5.Number of rows queried in this apex code so far: ' + Limits.getDmlRows());

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Avidev9Avidev9

Well I guess I answered somewhere else!

 

This shows you how to calculate the number of SOQL for batch apex and insert the same in a custom object 

 

 

  • If you want to insert info about each batch
global class MyBatch implements Database.Batchable<sObject>{
 
   global MyBatch(){
        
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Your Query');
   }

   global void execute(Database.BatchableContext BC, 
                       List<Sobject> scope){
//Should be the last line
MyCustomObject__C obj = new MyCustomObject__c(Name='My-Batch',SOQL__c=Limits.getQueries());
insert obj; } global void finish(Database.BatchableContext BC){ } }
  • Now say you want to record the number of queries for whole batch process(since a batch is broken into smaller batch of 200 each)
global class MyBatch implements Database.Batchable<sObject>,Database.statefull{
   private static Integer queryCount = 0;
   global MyBatch(){
        
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Your Query');
   }

   global void execute(Database.BatchableContext BC, 
                       List<Sobject> scope){
//should be the last line
queryCount = queryCount + Limits.getQueries(); } global void finish(Database.BatchableContext BC){
MyCustomObject__C obj = new MyCustomObject__c(Name='My-Batch',SOQL__c=queryCount);
insert obj;
} }

 

 

samrat.1985@lntinfotechsamrat.1985@lntinfotech

Thanks for you replies.

 

Consider a situation when the code with batch apex is already implemented with a VF page using the controller. Can i now calculate the No of Queries issued for Batch Apex in that page controller using a new controller whitout changing any code in the existing controller.