2

I wrote a batch with the querylocator string as below

SELECT ID ,(  SELECT 
                    Id,
                    PermissionSet.Id, 
                    PermissionSet.Name 
              FROM 
                    PermissionSetAssignments  
              WHERE 
                   PermissionSet.IsOwnedByProfile = false )  
FROM User 
WHERE isActive = True 

I have user 35k and permission set Assignment count is 2916163. My batch size is 1. Any suggestion to make it success run.

This is the error after some time in monitoring the apex job log.

Batch Apex Failed First error: Apex CPU time limit exceeded

Note : I have to run this for each user.

1
  • 4
    Try to separate child query and instead do it in execute method...In my opinion that would solve your issue.
    – Mr.Frodo
    Dec 21, 2016 at 12:37

2 Answers 2

6

The CPU time limit was exceeded because you have a non-selective query. The query you're using requires that every record to be checked in order to return a result. That's very, very slow. You may just as well be returning all of those records (much quicker) and then sorting through them in your execute method.

2
3

In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query. These types of queries have a limit of three times the number for top-level queries. The row counts from these relationship queries contribute to the row counts of the overall code execution. In addition to static SOQL statements, calls to the following methods count against the number of SOQL statements issued in a request.

  • Database.countQuery
  • Database.getQueryLocator
  • Database.query

Try to Optimize the batch by removing the child query from the Start and put that in the execute method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .