The below query runs successfully sometimes but fails with a query time out issue Fault code [QUERY_TIMEOUT]. Reason [Your query request was running for too long.]. sometimes. Is using an IN clause the issue? What are the alternatives please?

SELECT AccountId ,CreatedDate ,OldValue ,NewValue ,Account.CORE_Account_number__c 
FROM AccountHistory 
Where (AccountHistory.Field = 'Type' AND 
  AccountHistory.CreatedDate >= 2016-10-01T00:00:00.000Z And 
  AccountId in 
    (select Opportunity.AccountId 
     from Opportunity 
     where CloseDate >= 2016-10-01 and 
    CloseDate < 2016-11-01)
  • find all accountHistories post 2016-10-01 for Accounts with opportunities that closed in October 2016? That is, you want all history changes from 2016-10 through now on those Accounts? – cropredy Nov 3 '17 at 23:15

You appear to be querying a LOT of data (looking at 13 months of history records) - do you really need to consider all of that? (Did you mean 2017 vs 2016 in your example?) In the past, I've had to break up queries like this into smaller chunks to get things to work.

If helpful, SF support routinely sends me this when I'm working on tuning queries: https://help.salesforce.com/articleView?id=000181277&language=en_US&type=1

  • Thank you for your response. As I mentioned, the same query does not time out always irrespective of how much data it is pulling. We have increased the salesforce connection timeout value from 5 minutes to 15 minutes. Will that help resolve the issue permanently? – Vasantha Peddireddy Nov 6 '17 at 21:40
  • Right. Salesforce caches data, so it's not always going to the database. I have had some simple queries before that time out, and if you just wait 30 seconds and try them again, they'll work. It's just using cache is what's causing it to work okay. Like @cropredy is alluding to above, your query seems possibly incorrect and pulling too much data. – James Boutcher Nov 7 '17 at 23:02

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.