I have a Batch iterating over a large amount of records (with many fields) and 2 of their related child records. The start()
method uses Dynamic SOQL to fetch them via QueryLocator to vary which fields are queried.
global Database.QueryLocator start(Database.batchableContext context) {
programTypeFacade.setRunningBatch(context.getJobId());
String soql = Utils.getSelectXFrom('Volume__c');
String relationSubQueries = ', (SELECT num_Volume_01__c, num_Volume_02__c, num_Volume_03__c, chk_LockVolume_03__c ' +
' FROM VolumeSchedules__r)' +
', (SELECT mdr_Volume__c, lkp_VolumeData__r.txt_Mnemonic_1__c, lkp_VolumeData__r.pkl_Status__c, lkp_VolumeData__r.' + String.join(programTypeFacade.relevantAttributeFields,', lkp_VolumeData__r.') +
' FROM VolumeDataLinks__r)';
soql = soql.replace('FROM', relationSubQueries + ' FROM');
soql += ' WHERE mdr_ProgramType__c = \'' + programTypeFacade.Id + '\'';
return Database.getQueryLocator(soql);
}
and this is the Batch Apex log output
SELECT ...FROM Volume__c
16:57:12.863 (863677615)|SOQL_EXECUTE_END|[44]|Rows:21357
17:01:28.956 (256956271398)|SYSTEM_METHOD_EXIT|[33]|LIST<UP2GO_2F__Volume__c>.size()
17:01:28.956 (256956299794)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
17:01:28.956 (256956366758)|SYSTEM_METHOD_ENTRY|[33]|LIST<UP2GO_2F__Volume__c>.size()
17:01:28.956 (256956378599)|SYSTEM_METHOD_EXIT|[33]|LIST<UP2GO_2F__Volume__c>.size()
...(hundreds of equal lines...
17:03:43.785 (391785456348)|SYSTEM_METHOD_EXIT|[38]|LIST<UP2GO_2F__Volume__c>.size()
17:03:43.785 (391785470977)|SYSTEM_METHOD_ENTRY|[39]|Database.QueryLocator.querymore(Integer)
17:03:43.973 (391973215599)|EXCEPTION_THROWN|[39]|System.LimitException: Apex CPU time limit exceeded
Is this like a Query Timeout? Can I prevent this and how?
EDIT: This must be a bug a the query result is really small: 32.000 records with max. 200 child records. If I run the same query in the console the result comes back in a second without any problems.
hundreds of equal lines...
comment in the code could suggest a possible issue. If you are comparing SObjects using==
then you could hit the CPU limit, e.g.if (objectA == objectB)