We're experiencing a very annoying issue with a Batch job processing "Large Data Volume" (1.5M records over a table containing 4.5M records). Our batch start method last statement is: return Database.getQueryLocator(soql);
and despite the query completes successfully returning 1.5M rows, the batch doesn't kick off and a CPU timeout error is reported.
I believe the CPU timeout is not to be attributed to any of our logic, being the query our last code statement and completing it successfully (look at the pseudo-code, debug log and screenshots of CPU timeline and Apex Jobs provided below). I have the strong suspect the issue is related to the "housekeeping" sfdcfox mentions in here - but that's just a weak suspect as if that'd be the case I suppose I wouldn't be the only one experiencing the issue.
We have optimized the SOQL so that only the Id field of targeted records is selected: the SOQL doesn't contain any child/sub-query, and the records returned by the SOQL are as lightweight as they can be being only their ID field selected. This is a known factor that affects badly batch start() methods with CPU timeouts, but it's not to be considered by us after optimization. I wonder then if there are others.
The question (and bounty) is out for a clear, documented, detailed answer to the question: what are factors that can affect/impact on the post-processing that SFDC does on the recordset which is returned by a batch start() method, other than the presence of child queries or parent records?
Pseudo code that gives an idea of my batch
global with sharing class APseudoBatch {
private String soqlString = 'SELECT alias.Id FROM MyObject__c alias WHERE ...';
global Database.QueryLocator start (Database.BatchableContext BC) {
MyOtherObject__c[] prevBatchData = [SELECT Id FROM MyOtherObject__c WHERE ...];
delete prevBatchData;
return Database.getQueryLocator(soqlString);
}
(...)
}
Debug log
34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
05:12:19.152 (152753319)|SOQL_EXECUTE_BEGIN|[159]|Aggregations:0|SELECT id FROM AAA__c
05:12:19.152 (158857573)|SOQL_EXECUTE_END|[159]|Rows:0
05:12:19.152 (163217923)|SOQL_EXECUTE_BEGIN|[165]|Aggregations:0|SELECT id FROM BBB__c
05:12:19.152 (167389830)|SOQL_EXECUTE_END|[165]|Rows:1
05:12:19.152 (167994190)|DML_BEGIN|[405]|Op:Delete|Type:BBB__c|Rows:1
05:12:19.152 (401425184)|DML_END|[405]
05:12:19.152 (402249359)|SOQL_EXECUTE_BEGIN|[172]|Aggregations:0|SELECT id FROM CCC__c
05:12:19.152 (406737724)|SOQL_EXECUTE_END|[172]|Rows:4
05:12:19.152 (407218557)|DML_BEGIN|[405]|Op:Delete|Type:CCC__c|Rows:4
05:12:21.104 (2104679115)|DML_END|[405]
05:12:21.104 (2110622491)|DML_BEGIN|[409]|Op:Upsert|Type:DDD__c|Rows:4
05:12:21.104 (2183712913)|DML_END|[409]
05:12:21.104 (2192886624)|SOQL_EXECUTE_BEGIN|[188]|Aggregations:0|Select s.Id From LDV__c
05:13:25.773 (66773435973)|SOQL_EXECUTE_END|[188]|Rows:1517052
(Line 188 is the last line of my start() method - the debug log ends there, I've pasted it all as it is)
Screenshot of Apex Jobs page
The SOQL completes successfully at 05:13:25, the Batch Job is marked failed at 05:16 in the Apex Jobs page. Strange, isn't?
Screenshot of Execution Timeline
LAST UPDATE
Changed the batch query removing all the WHERE criterias. The behavior of the CPU timeout changed - now it is happening 10 minutes after the SOQL failure (as the linked sfdcfox post suggests) instead of 2-3 minutes as per my previous debug logs. The table contains 3.5M records now.