minimize number of db queries