Print this page

SQL query to get the count() of more than 50,000 records

Knowledge Article Number 000199472
Description Getting the count() on the number of records in a table having more than 50,000 records gives "System.LimitException: Too many query rows: 50001"
Resolution There are many solutions to resolve the "System.LimitException: Too many query rows: 50001" error . One of the solutions is to use Visualforce page readonly attribute and the example code is as follows: 

Visualforce page Code 
<apex:page controller="MyCountEx" showheader="false" sidebar="false" readOnly="True"> 

Controller Code 

public class MyCountEx{ 

public Integer myCount {get{ return countMethod();} } 

public static Integer countMethod() { 
Integer var = 0; 
var= [select count() from account]; 
return var; 


The limitation in using visualforce page is it is limited to 1 million records


promote demote