HHH-9455 : Unnecessary select count query in some cases

(cherry picked from commit 552c1c06d513bd066c0a3c6386119ea33e083cf7)