I had to make a few modifications to the above set since it would have still resulted in multiple queries per profile_hash. It is a se of set([('repo1', ('<profile hash>', '<content_type>'))]) now and there is a separate map of profile_hash mapping to a profile_id.