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
Jefferson escobar-pereaJefferson escobar-perea 

Query Aborted Automatically in Apex Batch Job

Hi all,

I have the following query that is executed by Apex Batch:

"Select Id, Cycle_BI__c, Matrix_BI__c, Matrix_1_BI__c, Matrix_2_BI__c, Matrix_3_BI__c, Matrix_4_BI__c, Matrix_5_BI__c, Account_BI__c, LastModifiedById, UniqueKey_BI__c From Cycle_Data_BI__c where isDeleted = false and LastModifiedById in ('005d0000001tDYU') and Account_BI__r.Country_Code_BI__c in ('US') And  Cycle_BI__r.IsCurrent_BI__c = true "

The jobs is aborted very often because the query takes too much time in being executed. 
 
"First error: SQLException [common.exception.SfdcSqlException: ORA-01013: user requested cancel of current operation
select /*ApexBatch.Class.IMP_BI_ClsBatch_DeletionDupsOnCycle.start: line 44*/ *
from (select "Id"
from (select /*+ ordered use_hash(t_"

The object has big amount of records but the condition applies for some of them, the fields in the query are already indexed and I've added different conditions in a different ways in order to optimize the query without success.

Any insight or suggestion for optimizing my current query ?

Many thanks, I really appreciate your support on this.
Suraj GharatSuraj Gharat

If there's just one user id to match against for "LastModifiedById" then you may use "=" operator instead of "IN", Or anyway this would a subtle change.

ll'y you may do for "Account_BI__r.Country_Code_BI__c" as well.
Jefferson escobar-pereaJefferson escobar-perea
The query is built dinamically that's why I am using the cluase IN. Can be multiple users and multiple countries. 
Jefferson escobar-pereaJefferson escobar-perea
I already solved the issue adding two new conditions to my query , isDeleted = false and LastModifiedDate = THIS_YEAR. The job runs in the daily basis therefore I can always delete the last records up to date.