Reverse query optimization