HHH-9455 : Unnecessary select count query in some cases

(cherry picked from commit 552c1c06d513bd066c0a3c6386119ea33e083cf7)
(cherry picked from commit 460e9662146dbeda74c2cbb54d6964baf7c4f610)