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
Gaurav AgnihotriGaurav Agnihotri 

Too Many Query Rows:50001

Hello, 
 I am in a process of understanding developer console log and the best and fastest way to resolve "Too Many Query Rows:50001" error.

In general there seems to be two methods to diagnose this issue. 
1. filter the log on SOQL_EXECUTE_LOG to track the result of each query
2. Execute limit on Execution overview to track result of each SOQL query used in the code.

I tend to lean toward number 2, but i could be wrong. what would you guys recommend?
is there any other place in the developer console where i can look for this information faster.

Thanks in advance, 
Gaurav
Best Answer chosen by Gaurav Agnihotri
Amit Chaudhary 8Amit Chaudhary 8

The exception is being thrown because the maximum number of rows you can return in SOQL calls in Apex is 50000.
 
You will need to use Batch Apex, in which the 50k limit counts per batch execution
These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method

Can you limit the results by adding either some more where criteria or using a LIMIT 50000 statement in the soql.

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8

The exception is being thrown because the maximum number of rows you can return in SOQL calls in Apex is 50000.
 
You will need to use Batch Apex, in which the 50k limit counts per batch execution
These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method

Can you limit the results by adding either some more where criteria or using a LIMIT 50000 statement in the soql.

 
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
Thanks for your reply. The requirement is such that I have to get results in realtime. Therefore, I can not use batch apex. I am trying to filter the log on SOQL_EXECUTE_LOG  but it does not show me the SOQL that is causing this issue. Is there any other way i could find the component that is returning an unexpected number of rows?
Amit Chaudhary 8Amit Chaudhary 8
Please post your code in which you are getting same issue. And check same code where you are USING SOQL without filter and limit
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
I did that and found execute limit on execution overview to be better. It displayed the SOQL that was returning too many rows. It was very good. Very good feature.