optimizing some queries