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
GunnarGunnar 

Group By too many query rows. Only in Apex. Not in console

I have a SOQL query.
Ran it in the Dev Console in our production and got a full years worth of results - just what I needed.
Did NOT get 'too many query rows' error in console.

So I thought I was good to go and programmed up the class module to get the data.

Run the apex program containing the exact SQL and get too many query rows.

How do I know it's exact?  Because I assemble the SQL string and system.debug it. Then get the SQL and paste into Dev Console.

So question?  Why 'too many query rows in apex, but not in Dev Console?

Marty C.Marty C.

Hello, Gunnar, it's odd that you would see different behavior in the Dev Console vs. actual execution. Instead of using System.debug(), can you look in the Apex debug log for the SOQL_EXECUTE_BEGIN marker (shown below)? This debug line should tell you the exact query executed by Salesforce. Try running that query in the Dev Console and comparing the results.

SOQL_EXECUTE_BEGIN

GunnarGunnar
Thanks. But ...

I run the Apex in the Developer console. The system.debug gives me the SQL.
Then I fail on  'too many query rows'.

Get the SQL from the system.debug.
Then go to the Query Editor in the Developer console.
Paste in the SQL and ... (1) nothing appears in the log when you run SQL from the Query Editor, (2) I get all the records back I expect and (3) no error.

Thanks, but this looks like a bug in SFDC. I'll open a ticket with them. Cheers.
GlynAGlynA
In my experience, Developer Console will return pretty much any number of rows - hundreds of thousands.  However, in synchronous apex code the limit is 10,000 rows per query and 50,000 rows per transaction.

Depending on what you're trying to do, you could use Batch Apex to access up to 50M rows.  If you need to display records on a VF page, you could use the "readonly" attribute, which relaxes the query restriction to 1M rows (I suspect this is how Dev Console does it).  If you need to display and possibly update records, you can't use readonly, but you could get up to 50K records by doing 5 separate queries of 10,000 rows.  In each query, ORDER BY Id, where the Id > the Id of the last record in the previous query.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon
GunnarGunnar
Thanks. Yeah. I'm well aware of the limitations and batching.
I was curious how I got away with it in the Developer Console.

Was testing the SOQL to get the right records back and discovered this.
Would be nice if the developer console also gave you the error and told you how many records were touched in a GroupBy.
But there are lots of things that "Would Be Nice" in SFDC that have been taken for granted since the early 90's in other databases.
:-)