implement query optimization