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
Tom PolitowskiTom Politowski 

Batch query on EmailMessage object times out

Batch Apex query on EmailMessage times out. Only 1M records in database. Query from Developer Console times out at 'LIMIT 10000'. Other queries on other objects with large 1M+ records work without issue. 
Code:
String query = 'SELECT Id FROM EmailMessage';

    global Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }   

How can I create query to return all records? Tried adding 'WHERE IsDeleted = false' - still times out.
NagendraNagendra (Salesforce Developers) 
Hi Tom,

Instead of using a normal Query, I suggest you create a Bulk Query. It's another Operation which is used for processing a large number of records.

Please let us know still if you are facing the issue.

Thanks,
Nagendra.
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Tom Politowski,

May I request you to please refer the below link for reference. I hope it will be helpful.

Best Regards
Rahul Kumar
Tom PolitowskiTom Politowski
Nagendra, Bulk queries are for external processes - we are in the same org.
Rahul, we do not do any sub-queries.

Why does the GetQueryLocator with no filtering timeout on EmailMessage - it works fine on other large recordsets?
Cory Tewksbury 4Cory Tewksbury 4
Tom, did you ever get an answer to this question.  I'm running into similar issues.
Tom PolitowskiTom Politowski
I found the answer is that because of the large size of the Email Message and Attachment we reduced our queries to batches of 50 with no filtering and did the filtering in the program and then re-queried the email Messages, Tom Politowski A4 International, Inc. 949 313-8530 x710 tomp@a4intl.com