LPS-45839 Optimize inefficient pagination logic