In the developer console query editor, if I enter the query

select lastmodifiedDate from foo__c order by lastmodifiedDate desc limit 1

it runs fine. But the moment I try do that same query inside of a controller, I get an Internal Server Error. Debug logs show

14:44:11.956 (103956172471)|SOQL_EXECUTE_BEGIN|[31]|Aggregations:0|select lastmodifiedDate from foo__c order by lastmodifiedDate desc limit 1
14:46:15.080 (227080647980)|FATAL_ERROR|Internal Salesforce.com Error
14:46:15.080 (227080675619)|CUMULATIVE_LIMIT_USAGE
14:46:15.080 (227080675619)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 4 out of 100
  Number of query rows: 57 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 100 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

As you can see from the log above, it says the soql query takes over 2 minutes before returning the error. Why is my query failing inside a controller?

  • what i am guessing because of your heep size exceed – Ratan Paul Nov 27 '15 at 4:01
  • do soql queries have a hidden heap size limit? – user2610036 Nov 27 '15 at 4:04
  • looks like it is not a code level error. Try after sometimes may be this will resolved. – Ratan Paul Nov 27 '15 at 4:07
  • is your query dynamic means you are using database.query() or Database.getQueryLocator() ? – Ratan Paul Nov 27 '15 at 4:10
  • I'm doing it in a database.query() – user2610036 Nov 27 '15 at 4:14

I think its performance issue. The query is running super slow. There are multiple options to optimize this query.

1) use SystemModStamp : https://developer.salesforce.com/blogs/engineering/2014/11/force-com-soql-performance-tips-systemmodstamp-vs-lastmodifieddate-2.html

2) Make query more selective by adding some criteria . i.e. where SystemModStamp > THIS_MOnths : https://developer.salesforce.com/blogs/engineering/2013/12/force-com-query-optimizer-secrets-you-can-use-today.html

3) Store the timestamp when you update your record.

  • In addition to these suggestions, I've found that using the ajax toolkit to do the query is more performant, even though it should have the same governor limits. – user2610036 Dec 1 '15 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.