I'm current first getting all friends of a certain user and then take the size of the array as the number of friends. My concern is, since every time I'm retrieving all friends information from the database, it is potentially (or obviously) inefficient. So I'm wondering if there's a way that could query the number of friends of a certain user effectively without getting any other information?
If your relations are declared correctly, you should be able to do
See here in my console the SQL queries generated (
In your case, if you have your relations like this:
Then this should be executed at the DB-level and be faster than your first piece of code: