Too many query rows 50001 with aggregate function

11:24 AM

Problem:

You get this exception message on a simple aggregate query like this.

Select Status from Case GROUP BY Status - FAILS

You try to use a limit clause but the query still fails.

Select Status from Case GROUP BY Status LIMIT 1000 - STILL FAILS

 

Reason:

Even though you may use a limit clause, aggregate queries will have to touch all records in the database. If you have less than 50000 Cases you would not receive this error message.

Solution:

Couple of probable solutions.
  1. Try to limit query results using a WHERE clause.
  2. If using this on a visualforce page and you do not perform any DML (insert, update etc) from the page, set readOnly=true on the apex:page tag.
  3. If using this on a visualforce page and you need to perform DML operations, use @RemoteAction and @ReadOnly on your apex class and deal with the results using javascript. Example link. 

0 comments