Issue #2668730: Change batch to query database once and work on the entity IDs in order to avoid looping.