Get ONLY the number of results in a query

There are three sides for the “efficiency” issue.

From the DB side, most of the time is spent in traversing the DB tables. If you need all the rows, you need the DB to traverse all of them no matter what information will be returned. There is probably a cost associated with holding data in memory, but it should be negligible relative to the traversing cost.

For the communication side, it is obvious that the smaller the information being transferred is, the faster the transfer will be completed. It is very hard to guess what the impact is as it will depend on if the DB is on the same host, and if it is not on the quality of the network. This is something you can and should handle by either using ‘fields’ => ‘ids’ as you have done, or by creating your own SQL query that will return only the number. IMO the ‘fields’ => ‘ids’ is more in line with how wordpress code is expected to work, and even though it is not optimal it should be the preferred way.

From the wordpress side, if you do not specify some specific set of field to return like ‘fields’ => ‘ids’, wordpress by default will generate queries that will prime the taxonomy and meta caches which relate to the returned post, so if you know that you are not interested in that information you should “turn it off”. The relevant flags are cache_results that can be use to disable all relevant caching, or the more specific update_post_meta_cache and update_post_term_cache