Maybe do this right away in one query?
That saves a lot of performance on the database..