Yes you are doing a nice way to get all messages from community by using count query first.
Are you going to show this somewhere in your community? If YES, then you will mess-up with performance issue on community for this page.
If you are doing this for export messages, try to make it using limit the query and add page_size for next numbers of results in the your query.
Welcome to the Technology board!Curious about our platform? Looking to connect on social technology? You've come to the right place!