optimized compound query