"ProcessInstancesWaitingForEvent" query that is used after each process / subprocess ends is extremely inefficient, and since there is no way to force an INDEX without being coupled with JPA provider, this query execution time increase exponentially as the number of processes goes up. Currently with only 20000 processes, the query takes 18(!!!) seconds to execute:
FROM ProcessInstanceInfo processInstanceInfo
WHERE :type in elements ( processInstanceInfo.eventTypes )
It is inefficient not only because there is no INDEX enforced, but because it goes into the sub query for no good reason. The better/correct query is:
FROM ProcessInstanceInfo processInstanceInfo, EventTypes eventTypes
WHERE eventTypes.element = :type
AND processInstanceInfo.processInstanceId = eventTypes.processInstanceId
In order to get to this "better" query, since it is a JPA one, an EventType/s entity should be created ( it does not currently exist in 5.1.1 ).