Count of Records in Certain Result Sets

You can use the getRowCountIfPossible() function to get the count of records in a result set when using Query Functions.

Query.ResultSet.getRowCountIfPossible()

This function can be used only with snapshot queries that use joins and aggregations (order by and group by clauses). Only in such cases is the result set known. In other cases the query begins filtering and feeding results to the result set without knowing when the query will end.