// Tested this with ~140k products - compared with pure SQL this is reasonable fast
// In most cases the filter query will be the bottleneck
$i = 0; $model = null; foreach($resultsas$detailId){ // Flush after 20 entities
if(($i++ % 20) === 0){ $entityManager->flush($model); $entityManager->clear();