Patch/optimize query