Optimize queryies for api