Speed up get_perms

Split a single large permissions query into multiple queries to improve
performance