Fix the inefficient query.