Apex Job failure: SQLException ORA-01013 user requested cancel of current operation thrown but connection was canceled
|Knowledge Article Number||000175555|
|Description||SQL Exception error is encountered when running the "ABC" batch job "First error: SQLException [java.sql.SQLException: ORA-01013: user requested cancel of current operation] thrown but connection was canceled". The job is then aborted.|
|Resolution||"ORA-01013: user requested cancel of current operation" is a Batch Apex UI bug. A user friendly error message should be displayed instead, clarifying that the query was aborted since it took longer than 2 minutes to complete.
For a query to take longer to run you don't need to make any code changes. In fact the same query may work in different orgs, as the important factor is the distribution data. In other words, the same query may run fine in a sandbox where the total number of records (including soft-deleted ones) is X, but may time out in production where the total number of records is Y, where Y is greater than X.
The reason for this is that if a query is selective it will run faster as it will be index driven, if a query is unselective it will not be index driven and a full table scan will be necessary, which requires time proportional to the amount of records.
A query is selective when at least one filter returns less than 10% of the records up to 333k AND it is based on an indexed fields. There are standard indexed fields (like lookups, master detail relationships, unique, external id, and audit fields) and custom indexed fields (indexed by Salesforce).
You can use the Query plan tool to determine the selectivity of SOQL Query.