Optimization grid query