ORM optimization

Hi I have a query, I have two tables Posts and Likes. I’ve millions of posts, Fine, And each post can have millions of likes, OK. Now I want to retrieve those posts along with the count of likes for each post (I can do pagination etc but that’s not my topic). Is doing the following a better choice???

Likes.objects.filter(post_id=someid).count()

Or won’t it retrieve all the columns also of the table. Should I write raw sql instead on indexed column.

SELECT COUNT(id) FROM LIKES where postid = someid;

Please let me know.

Also when select_related() for make our queries slow? Since it’s doing heavy JOINS under the hood isn’t. (ex we have a huge data, and want to avoid joins, rather to get the city and it’s state data, we’ll find state id from city data (retrieved from DB) and then find state from state table (DB).

I’m not sure what should I implement as I’m yet learning things. Any sort of help will be very much appreciated here.
Thanks

If you look at the query being emitted you’ll see that the ORM expression you’re showing generates the same SQL as the sample you’ve provided.

If this sort of issue is something that is going to be a concern for you, I suggest you become familiar with looking at the connection.queries object in django.db in the Django shell, and to logging queries being issued.