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) )