rewriting sql optimization in light of non-grid data