Optimized the orphans query