How to efficiently sort by the results of a subquery?
问题 Let's say I have a site like Stackoverflow with posts that people can reply to, and I would like to have an interface for sorting the posts by reply count This will eventually have infinite scroll pagination, so showing 10 results at a time. Here's an example query for that: SELECT *, (SELECT COUNT(*) FROM post_reply pr WHERE pr.convo_id = post.convo_id) as replies FROM post ORDER BY replies LIMIT 10; This works, but it is prohibitively slow. I have hundreds of thousands of posts and this