+ Start a Discussion
ThathulaThathula 

How to check how many queries have run so far

Hi EveryOne, 

I need to know how many queries have run so far. Is there any direct method to get that info?
I'm writting a test class for controllers

Thanks alot............
Amit Chaudhary 8Amit Chaudhary 8
You can use debug log in developer console.

In the bottom of page it will give you result like this

11:48:38.109|CUMULATIVE_LIMIT_USAGE
11:48:38.109|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 8 out of 100
  Number of query rows: 9 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 20 out of 150
  Number of DML rows: 20 out of 10000
  Number of script statements: 583 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:48:38.109|CUMULATIVE_LIMIT_USAGE_END

Please check below post to avoid limit
https://developer.salesforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits

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());

 Please let us k now if this will help you

Thanks
Amit Chaudhary
 
PoornaPoorna
Amit - For which apex class the below line will return the number of queries? It is for entire org? or any specifica apex class?

System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
Ankit PurohitAnkit Purohit
You can use this kind of condition:

if(Limits.getQueries() >= Limits.getLimitQueries())

Here Limits is a class to get the total number of queries fired till now by using getQueries() method. This function returns the total number of queries fired in the current execution, i.e. it will tell the total number of queries even if the execution has caused any trigger to initiate.

The other function getLimitQueries() tells us about the total limit imposed by Salesforce.

Do let me know if this helps you.
David Roberts 4David Roberts 4
System.debug('Total Number of script statements allowed in this apex code context: ' + Limits.getLimitScriptStatements());

getLimitScriptStatements Method was removed after version 30.0

The link to best practices now goes direct to the 'Limits' class definition.
An alternative thread with useful info is: https://developer.salesforce.com/forums/?id=906F0000000BZxqIAG.

The debug statements helped me find where my problem originated. Thanks, Amit.