simplify a complex query