Re #1692 prevent query repeats and not fetch rows which are not required

@jonpday please review