Optimize queries in exercise controller