mysql - How to make sql more efficient. Need just the count -
i need find number of users:
- who have 1 or more fans
- who have 2 fans
- who have 3 fans
here sql developed answer #1
select users.id, count(fans.id) users join fans on users.id = fans.user_id group users.id having count(fans.id) > 0
above query works getting user records , calculating size of array. has terrible performance when number of users in thousands. how refactor query count of users?
i using mysql, might need project using postgresql.
a subquery nicely.
select count(user.id) users (select count(fans.id) fans user_id = users.id) > 0
Comments
Post a Comment