I'm writing a query in postgres to select the posts with more comments.
The following works but I'm wondering if it could become a performance problem with many posts.
Query:
SELECT
po.*,
(SELECT count(id) FROM comments WHERE post_id = po.id) AS comments_count
FROM posts AS po
ORDER BY comments_count DESC
LIMIT 10;
Result:
id title body comments_count
2 Foo Bar 5
1 Click Bait 4
Is there something I can do to improve this query performance or is it ok?